An array is an indexed list of variables that you can specify in square brackets to access individual members of an array, where the index is an integer, starting with 0.
one-dimensional arraysmultidimensional Arrays (rectangular arrays)Array of arrays (jagged arrays)
The array must be initialized before access , there are two ways to initialize the array, you can specify the contents of the array literally, or you can use the New keyword to explicitly initialize the array;
Int[] arr = {1, 2, 3}; int[] arr1 = new Int[3];
An array type is a reference type derived from an abstract base type array. Because this type implements IEnumerable, you can use foreach iterations for all arrays in C #, and the Foreach Loop has read-only access to the array contents, so you cannot change the value of any element.
Int[] arr = {1, 2, 3}; foreach (var item in arr) { Console.WriteLine (item); } Array arr2 = array.createinstance (typeof (int), 3); Arr2. SetValue (1, 0); Arr2. SetValue (2, 1); Arr2. SetValue (3, 2); foreach (Var item1 in arr2) { Console.WriteLine (item1); }
Multidimensional arrays are arrays that use multiple indexes to access their elements, with two-dimensional arrays as an example:
int[,] arr = {{1, 2}, {4, 5}, {7, 8}}; int[,] arr1 = new int[3, 2]; Arr1[0, 0] = 1; Arr1[0, 1] = 2; foreach (var item in arr) { Console.WriteLine (item); }
The first digit in square brackets specifies the curly brace pair, and the second digit is the element in the curly brace
Jagged array: Each element in the array is another array, each row has a different number of elements,
int[] A =New int[2][]; a[0] =New int[3]; a[1] =New int[4]; int[] B =New int[2][] {New int[] {1,2,3},New int[] {4,5,6,7 } }; int[] C = {New int[3] {1,2,3},New int[4] {4,5,6,7 } }; //access to jagged arrays int[] A = {New int[3] {1,2,3},New int[4] {4,5,6,7 } }; foreach(varItemincha) {foreach(varItem1inchItem) {Console.WriteLine (item1); }} Console.WriteLine ("----------------------------------"); for(inti =0; i < a.length; i++) { for(intj =0; J < A[i]. Length; J + +) {Console.WriteLine (a[i][j]); } }
Initialization of jagged arrays:
The default value of a numeric array element is set to zero, and the default value of the reference element is set to NULL.
A jagged array is an array of arrays, so its elements are reference types and are initialized to null.
C # arrays, multidimensional arrays (cross arrays), jagged arrays (cross arrays)