Saturday, 5 May 2012

Arrays in C#

Arrays -- Store homogeneous data types. memory is allocated in continuous memory blocks
C# supports , single, multiple, jagged arrays.


Arrays can contain all value types & all reference types.
Value types are default initialized .(for ex: int & enum with 0 etc.,)
Reference types default initialized with null.


Single array:
         int[] arr = {1,2,3};  //single dimension array with 3 elements indexed with 0,initialized with values 1,2,3 .
              or  
          int[] arr2=new int[3]; //another syntax array with 3 elements indexed with 0, initialized with default value 0 for all elements.


Multiple Arrays


Sytanx
       int [,] marray=new int[2,2];


all elements in this array initialized with int default values . i.e 0.


for(int  i=0; i < i < marray.Get(0);i++)
{
    for(int j=0; j < marray.Get(1);j++)
   {
            marray[i,j]= i+(j*10);
   }
}




Jagged Arrays
---------------------
  Arrays of Arrays. Each Arrays elements point to an Array.
syntax
-------------
int [][] jarray = new int[2][2];
It has 2 elements each one point to another Single dimensional array.


syntax 2
-------------------
int[][,] jarray2 = new int [3][,]
jarray2 has elements each one point to one multi-dimensional array.





No comments:

Post a Comment