The following content is from MSDN
An array is an ordered collection of items that have the same data type. To access an item in an array, you need to use both the array name and the offset between the entry and the beginning of the array. In C #, the methods of declaring and using arrays have some important differences from Java.
One-dimensional arrays
A one-dimensional array stores a fixed number of items in a linear fashion, with just one index value identifying any item. In C #, the square brackets in an array declaration must be followed by a data type and cannot be placed after the variable name, which is allowed in Java. Therefore, arrays of type integers should be declared using the following syntax:
Int[] arr1;
The following declaration is not valid in C #:
int arr2[]; Compile error
After you declare an array, you can use the New keyword to set its size, which is the same as Java. The following code declares an array reference:
Int[] arr;
arr = new Int[5]; Create a 5 element integer array
You can then access elements in a one-dimensional array using the same syntax as Java. The C # array index is also zero-based. The following code accesses the last element in the array above:
System.Console.WriteLine (Arr[4]); Access the 5th Element
Class
C # array elements can be initialized at creation time using the same syntax as Java:
Int[] Arr2lines;
Arr2lines = new Int[5] {1, 2, 3, 4, 5};
However, the number of C # initializers must exactly match the size of the array, which is different from Java. You can use this feature to declare and initialize a C # array in the same row:
Int[] Arr1line = {1, 2, 3, 4, 5};
This syntax creates an array of size equal to the number of initializers.
Class in the program loop.
Another way to initialize an array in C # is to use a for loop. The following loop sets each element of an array to zero:
int[] taxrates = new INT[5];
for (int i=0; i<taxrates.length; i++)
{
Taxrates[i] = 0;
}
Jagged array
Both C # and Java support the creation of staggered (not rectangular) arrays, where each row contains a different number of columns. For example, in the following jagged array, the first row has four items, and the second row has three items:
int[][] Jaggedarray = new int[2][];
Jaggedarray[0] = new INT[4];
JAGGEDARRAY[1] = new INT[3];
Multidimensional arrays
You can use C # to create multidimensional arrays of rules, which are similar to matrices of similar types of values. Although both Java and C # support jagged arrays, C # also supports multidimensional arrays (arrays of arrays).
Declare a multidimensional rectangular array using the following syntax:
int[,] arr2d; Declare the array reference
float[,,,] arr4d; Declare the array reference
After the declaration is declared, you can allocate memory for the array as follows:
arr2d = new int[5,4]; Allocate space for 5 x 4 integers
You can then access the elements of the array using the following syntax:
arr2d[4,3] = 906;
Because the array is zero-based, this row sets the element in column fifth of line fourth to 906.
Class
You can create, set, and initialize multidimensional arrays in the same statement using one of the following methods:
int[,] arr4 = new int [2,3] {{1,2,3}, {4,5,6}};
int[,] arr5 = new int [,] {{1,2,3}, {4,5,6}};
int[,] Arr6 = {{1,2,3}, {4,5,6}};
Current 1/2 page
12 Next read the full text