Search This Blog

Thursday, December 20, 2012

Array C#

C# > Types > Array

An array is a data structure that contains a number of variables called the elements of the array.
C# arrays are zero indexed.
All of the array elements must be of the same type.
Array elements can be of any type, including an array type.

Single-Dimensional Arrays

int[] m_i = new int[3];
string[] m_s = new string[5];
int[] m_i = new int[] { 1, 2, 3 }; // Array Initialization

Multidimensional Arrays

int[,] m_i = new int[3,2]; // two-dimensional array of 3 rows and two columns:
int[,,] m_i_1 = new int [5,1,2]; //  array of three dimensions, 5, 1, 2
int[,] myArray = new  int[,] {{1,2}, {3,4}, {5,6} }; // Array Initialization

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.

int[][] m_j = new int[2][];

m_j[0] = new int[2];
m_j[1] = new int[3];