Arrays

Arrays
In C#, arrays are actually objects, and not just addressable regions of
contiguous memory as in C and C++. Array is the abstract base type of
all array types. You can use the properties, and other class members,
that Array has. An example of this would be using the Length property to
get the length of an array. The following code assigns the length of the
numbers array, which is 5, to a variable called lengthOfNumbers:

int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = numbers.Length;
Implicitly Typed Arrays
int[] a = new[] { 1, 10, 100, 1000 }; // int[]
var b = new[] { "hello", null, "world" }; // string[]

An array has the following properties:
An array can be Single-Dimensional, Multidimensional or Jagged.
The number of dimensions and the length of each dimension are
established when the array instance is created. These values can't be
changed during the lifetime of the instance.
The default values of numeric array elements are set to zero, and
reference elements are set to null.
A jagged array is an array of arrays, and therefore its elements are
reference types and are initialized to null.
Arrays are zero indexed: an array with n elements is indexed from 0 to
n-1.
Array elements can be of any type, including an array type.
Array types are reference types derived from the abstract base type
Array. Since this type implements IEnumerable and IEnumerable<T>, you
can useforeach iteration on all arrays in
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };

With multidimensional arrays, you can use the same method to iterate
through the elements, for example:

int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
System.Console.Write("{0} ", i);
}
// Output: 9 99 3 33 5 55

arrays can be passed as ref and out type arguments to a function

Jagged Array
// Declare the array of two elements:
int[][] arr = new int[2][];

// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };

// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);

for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? ""
: " ");
}
System.Console.WriteLine();
}