Home  >  Article  >  Backend Development  >  C# array declaration

C# array declaration

高洛峰
高洛峰Original
2016-12-16 14:59:292400browse

In C#, the declaration of an array is to instantiate the array.

1. Declaration of one-dimensional array

1. Integer array

The following declares a one-dimensional array with 5 integer elements:

int[] array = new int[5]; The above array contains the values ​​from array There are a total of 5 integer elements from [0] to array[4]. The new operator is used to create an array and initialize the array elements to their default values. In this example, all array elements are initialized to zero.

2. String array

string[] stringArray = new string[10]; The above array contains a total of 10 characters from stringArray[0] to stringArray[9].

2. Declaration of two-dimensional array

The syntax format for declaring a two-dimensional array is as follows:

Data type [,] array name = new data type [second dimension size, first dimension size]; for example, declare a Two-dimensional array with 5 rows and 3 columns:

int[,] array = new int[5,3]; 5 rows refers to the size of the second dimension in the two-dimensional array, and 3 columns refers to the first dimension in the two-dimensional array dimension size.

3. Declaration of three-dimensional array

The syntax format of declaring a three-dimensional array is as follows:

Data type [,,] array name = new data type [third dimension size, second dimension size, first dimension size]; for example:

int[,,] array = new int[4,5,3];

IV. Declaration of interleaved array

The following declares a one-dimensional array composed of 5 elements, where each element is a one-dimensional array. Dimensional integer array:

int[][] jiaoArray = new int[5][]; Traverse jagged array:

for(int x = 0; x < jiaoArray.Length; x++)
{
jiaoArray[x] = new int[4];
}

5. Mixed use of rectangular arrays and jagged arrays

The following code declares a one-dimensional array of a two-dimensional array of type int and a three-dimensional array:

int[][, ,][,] array;



For more articles related to the declaration of C# arrays, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn