Declaring an array and assigning an initial value
Int[] arr = {1, 2, 3};
You can see that [] followed by the type name is the array, {} represents the list of initial values to be assigned, and each initial value is separated by commas.
Declaring an array
int[] arr = new INT[3]; Correct, declare an array of type int of length 3 int[3] arr2; Error, cannot specify array size int[] ARR3; The array size is not specified and cannot be used
If we do not know the value of the array in advance, we can declare the array as described above, of course, when declaring an array, you should know the type and size of the array, and the number in [] indicates that the length is not the upper bound of the subscript.
C # Arrays also support dynamically specifying
int len = 3;int[] arr = new Int[len];
assigning values to arrays
int[] arr = new int[3];arr[0] = 10; Assigns an int m = arr[0] to the first element; Take the value of the first element
Get array length
int[] arr = new Int[3];int len = arr. Length;int len2 = arr. GetLength (0)//This method can also get the array length, which indicates the length of the array to get the number of dimensions, starting from 0.
More convenient for applications under the. NET Framework 3.5
The C # 3.0 syntax is used under the. NET Framework 3.5, so it is more convenient to use an array, without specifying the length and directly assigning values.
string[] colors = new string[]{"#333", "#666", "#999", "#ccc", "#fff"};
Declares an array of colors and assigns five strings to it. The length of the array here is determined automatically by the number of braces.