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 item and the start of the array. In C #, the method of declaring and using C # arrays has some important differences from Java.
One-dimensional arrays
A one-dimensional array stores a fixed number of items in a linear fashion, with an index value that identifies any one item. In C #, the brackets in the array declaration must follow the data type and cannot be placed after the variable name, which is allowed in Java. Therefore, an array of type integers should be declared with 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 in Java. The following code declares an array reference:
Int[] arr; arr = new Int[5]; Create a 5 element integer array
You can then access the 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 above array:
System.Console.WriteLine (Arr[4]); Access the 5th Element
Initialization
C # array elements can be initialized with the same syntax as Java when they are created:
Int[] Arr2lines; Arr2lines = new Int[5] {1, 2, 3, 4, 5}; Syue.com
But the number of C # initializers must exactly match the size of the array, which differs from Java. You can use this feature to declare and initialize a C # array on the same line:
Int[] Arr1line = {1, 2, 3, 4, 5};
This syntax creates an array whose size is equal to the number of initializers.
Initializing 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 the array to zero:
int[] taxrates = new Int[5]; for (int i=0; i< taxrates.length; i++) { taxrates[i] = 0; }
Jagged arrays
Both C # and Java support the creation of jagged (non-rectangular) arrays, that is, 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:
nt[][] Jaggedarray = new int[2][]; Jaggedarray[0] = new Int[4]; JAGGEDARRAY[1] = new INT[3];
Multidimensional arrays
You can create a multi-dimensional array of rules using C #, which resembles a matrix of homogeneous values. Although both Java and C # support jagged arrays, C # also supports multidimensional arrays (arrays of arrays).
Use the following syntax to declare an array of multidimensional rectangles:
int[,] arr2d; Declare the array reference float[,,,] arr4d;//Declare the array reference
After the declaration, 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 C # array is zero-based, this row sets the element in column fifth of row fourth to 906.
Initialization
You can create, set, and initialize a multidimensional array in the same statement by using one of the following methods:
int[,] arr4 = new int [2,3] {{}, {4,5,6}}; int[,] arr5 = new int [,] {{}, {4,5,6}}; int[,] Arr6 = {{}, {4,5,6}};