C# Arrays
C# supports single-dimensional arrays, multidimensional arrays (rectangular arrays), and array-of-arrays (jagged arrays).
Single Dimensional Array
When a list of items is identified by one variable name using one subscript, such a variable is called as one dimensional array.
Creation of an array involves 3 steps:
- Declaring array
- Creating memory location
- Putting values into the array
Syntax
DataType[ ] VarName=new DataType[size];
Example
Int[] n=new int[5];
Array Class
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.
Some methods of Array Class
| Method | Purpose |
| Array.Sort(VarName ) | Sorts an elements in one dimensional array |
| Array.Reverse( VarName) | Reverse the content of one dimensional array |
| Array.Copy(Source,Dest,length) | Copies one dimensional source array to destination array |
| VarName.GetValue(index) | Gets the value for a given index of the array |
| VarName.SetValue(Value,index) | Sets a value to given index in the array |
Two Dimensional Array
Two dimensional array can be declared as follows:
DataType[,] VarName=new DataType[RowSize,ColSize];
Jagged Arrays
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
Example
int[ ][ ] jaggedArray = new int[3][ ];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

Anil Kumar MurmuPosted May 27, 2014, 2:45 AM
@Leon: as per my understanding in jagged array, it can have 2D array. but then declarative syntax would be different. lets take the example give in article.int[ ][ ] jaggedArray = new int[3][ ]; //Here row size has been defined as 3 hence, we have added 3 single dimensional array i.e. as below.//In jagged array, row size is always fixed however, column size can vary.jaggedArray[0] = new int[5]; //0th index contain: 1 row but 5 columns// jaggedArray[1] = new int[4]; //1st index contain: 1 row but 4 columns// jaggedArray[2] = new int[2]; //2nd index contain: 1 row but 2 columns// However, if we are saying the statement as "jaggedArray[0] = new int[5,5];" then jaggedArray will again be having 5 rows and 5 columns inside it. it will violate the declaration made in 1st statement. new int[rowsize][] so for clubbing multi-dimensional array with jagged array, syntax would be little different as follow : int[][,] ar= new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} } }; For further clarification; please refer to link: http://msdn.microsoft.com/en-us/library/2s05feca.aspx
Leon PuthPosted May 21, 2014, 5:32 AM
can a jagged array contain two dimensional arrays i.e. jaggedArray[0] = new int[5,5]?