1. Simple array:
statement:
int[] MyArray
Initialize:
Once an array has been declared, it must allocate memory for the array to hold all the elements of the arrays. It is particularly important to note that arrays are reference types. It is therefore necessary to use the new operator to specify the type and number of elements in the array to initialize the variables of the array.
MyArray = new Int[4]
Declaration Initialization Merge:
int [] MyArray = new Int[4];
use an initializer to assign values to each element of an array:
It is important to note that the array initializer can only be used when declaring an array variable, and cannot be declared after the array variable.
int [] MyArray = new int[4]{2,3,4,5};
Suppose you initialize an array with curly braces. Also can not know the size of the array, the compiler will actively count the number of elements:
int [] MyArray = new int[]{2,3,4,5};
another simpler way:
int [] MyArray = {2,3,4,5};
array elements: passing the element number through the indexer. You can access the array; the indexer always starts with 0.
Assuming that the wrong indexer value is used, an exception is thrown IndexOutOfRangeException
declares an array consisting of two objects:
Pulic class person
{public string FirstName;
public string LastName;
}
person [] mypersons = new person[2];
The next thing to note is that the element in the numeric value is a reference type. You must allocate memory for each array element. If an element with unallocated memory in the array is used, an NullReferenceException exception is thrown.
Mypersons[0] = new Person{firstname = "Wang", LastName = "Moumou"};
mypersons[1] = new Person{firstname = "WU", LastName = "Meimei"};
, you can also use the initializer for your own definition type:
person [] MyPerson2 = {
New Person{firstname = "Wang", LastName = "Moumou"},
New person{firstname = "WU", LastName = "Meimei"}
};
2. Multidimensional Arrays:
to declare a two-dimensional array, you need to add a comma to the square brackets; The array should specify the size of each heap at initialization time
int [,] dim2 = new int[3, 3];
dim2[0,0] = 1;
dim2[0,1] = 2;
dim2[0,2] = 3;
dim2[1,0] = 4;
dim2[1,1] = 5;
dim2[1,2] = 6;
dim2[2,0] = 7;
dim2[2,1] = 8;
dim2[2,2] = 9;
Declare a three-dimensional array with two commas in square brackets
3. Jagged array
Like what:
0 S
3 4 5 6
7 8 9 int []
jagged = new int [3][];
Jagged[0]=new int[2]{1,2};
Jagged[1]=new int[4]{3,4,5,6};
Jagged[2]=new int[3]{7,8,9};
Use of arrays in C #