Tuesday, 10 July 2012

Tenth Class

Tenth class (Array, foreach loop and command line parameter)

Array : its a reference type data type.
Define : An array is a collection of values of similar types.
store memory in Heap its bcaz reference type data type store in heap and value type data type store in stack.


1. Syntax
             datatype [] arrayname;
2. Declare
             int [] arr;


Here int is data type.
[] you can say it subscripts or index or Rank or Dimension


How to intialize an array ?


There are four type to initialize array


1.

int [] arr=new int[5];
arr[0]=2;
arr[1]=34;
arr[2]=22;
arr[3]=33;
arr[4]=55;



2.

int [] arr=new int [] {2, 34, 22, 33, 55};



3.

int [] arr = new int [] {2, 34, 22, 33, 55};




4.

int [] arr = {2, 34, 22, 33, 55};



Example 1:


using System;
class array
{
public static void Main()
{
int [] arr=new int[5];
arr[0]=2;
arr[1]=34;
arr[2]=22;
arr[3]=33;
arr[4]=55;
Console.WriteLine("Value of first index is "+arr[0]);
}
}

                                                          foreach Loop



Syntax    foreach(Data Type Name of variable in Name of variable)

Example foreach(string str in args)

Here 'in' is a keyword

Here transfer the all value from args to str


             Example of foreach and Command Line Parameter


Command Line Parameter



using System;
class Car
{
public static void Main(string [] args)
{
foreach(string str in args)
{
Console.WriteLine(str);
}
}
}



                            How to Copy array







using System;
class arr
{
public static void Main()
{
int [] a=new int[4];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
int[] b=a;  //transfer all data of a to b.

               Console.WriteLine("third index value of an array is :"+b[2]);
}
}

BROUGHT TO YOU BY : www.hamarasem.com

No comments:

Post a Comment