Array Overview
C# arrays are indexed from zero, that is, array indexes start from zero. Arrays work in C# in similar ways as they work in most other popular languages. But there are some differences that should be paid attention to.
When declaring an array,Square brackets ([]) must be followed by the type, not by the identifier. In C#, putting square brackets behind an identifier is illegal syntax.
int[] table; // not int table[];
Another detail is,The size of an array is not part of its type, but in C it is part of the array type. This allows you to declare an array and assign it any array of int objects regardless of the array length.
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
Declare an array
C# supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays) and arrays (interleaved arrays). The following example shows how to declare different types of arrays:
One-dimensional array:
int[] numbers; multi-dimensional array:
string[,] names; array of array (interleaved):
byte[][] scores; declares arrays (as shown above) do not actually create them. In C#,Arrays are objects(This tutorial is discussed later), instantiation must be performed. The following example shows how to create an array:
One-dimensional array:
int[] numbers = new int[5];
Multidimensional array:
string[,] names = new string[5,4];
Array of arrays (interleaved):
byte[][] scores = new byte[5][];
for (int x = 0; x < ; x++)
{
scores[x] = new byte[4];
}
There can also be larger arrays. For example, there can be a three-dimensional rectangular array:
int[,,] buttons = new int[4,5,3];
You can even mix rectangular arrays and interlaced arrays. For example, the following code declares a one-dimensional array of three-dimensional arrays of type int.
int[][,,][,] numbers;
Initializing array C# provides a simple and straightforward way to initialize arrays when declared by enclosing the initial value in braces ({}). The following example shows various methods for initializing arrays of different types.
NoticeIf the array is not initialized at declaration, the array members are automatically initialized to the default initial value of that array type. Also, if an array is declared as a field of a certain type, it will be set to the default value when the type is instantiated null。
One-dimensional array
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
The size of the array can be omitted, as shown below:
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
If an initializer is provided, the new operator can also be omitted, as follows:
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};
Multidimensional array
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
The size of the array can be omitted, as shown below:
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
If an initializer is provided, the new operator can also be omitted, as follows:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
Interleaved array (array of array)
The interleaved array can be initialized as shown in the following example:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
The size of the first array can be omitted, as shown below:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
-or-
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Note that there is no initialization syntax for elements of interleaved arrays.
Accessing array members
Accessing array members can be done directly, similar to accessing array members in C/C++. For example, the following code creates an array named numbers and assigns 5 to the fifth element of the array:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;
The following code declares a multidimensional array and assigns 5 to members located in [1, 1]:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
The following declares a one-dimensional interleaved array that contains two elements. The first element is an array of two integers, and the second element is an array of three integers:
int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}
};
The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:
numbers[0][0] = 58;
numbers[1][1] = 667;
Arrays are objects
In C#, arrays are actually objects. is an abstract base type for all array types. You can use properties that have them and other class members. An example of this usage is to use the Length attribute to get the length of an array. The following code assigns the length of the numbers array (5) to a variable named LengthOfNumbers:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = ;
Classes provide many useful other methods/properties, such as methods for sorting, searching, and copying arrays.
Use foreach for arrays
C# also provides foreach statements. This statement provides a simple and clear way to iterate over elements of an array. For example, the following code creates an array called numbers and loops through the array with a foreach statement:
int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
foreach (int i in numbers)
{
(i);
}
Since there are multi-dimensional arrays, the same method can be used to iterate over elements, for example:
int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers)
{
("{0} ", i);
}
The output of this example is:
9 99 3 33 5 55
However, thanks to multi-dimensional arrays, using nested for loops will give you more control over array elements.
C# arrays are indexed from zero, that is, array indexes start from zero. Arrays work in C# in similar ways as they work in most other popular languages. But there are some differences that should be paid attention to.
When declaring an array,Square brackets ([]) must be followed by the type, not by the identifier. In C#, putting square brackets behind an identifier is illegal syntax.
int[] table; // not int table[];
Another detail is,The size of an array is not part of its type, but in C it is part of the array type. This allows you to declare an array and assign it any array of int objects regardless of the array length.
Copy the codeThe code is as follows:
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
Declare an array
C# supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays) and arrays (interleaved arrays). The following example shows how to declare different types of arrays:
One-dimensional array:
int[] numbers; multi-dimensional array:
string[,] names; array of array (interleaved):
byte[][] scores; declares arrays (as shown above) do not actually create them. In C#,Arrays are objects(This tutorial is discussed later), instantiation must be performed. The following example shows how to create an array:
One-dimensional array:
int[] numbers = new int[5];
Multidimensional array:
string[,] names = new string[5,4];
Array of arrays (interleaved):
Copy the codeThe code is as follows:
byte[][] scores = new byte[5][];
for (int x = 0; x < ; x++)
{
scores[x] = new byte[4];
}
There can also be larger arrays. For example, there can be a three-dimensional rectangular array:
Copy the codeThe code is as follows:
int[,,] buttons = new int[4,5,3];
You can even mix rectangular arrays and interlaced arrays. For example, the following code declares a one-dimensional array of three-dimensional arrays of type int.
int[][,,][,] numbers;
Initializing array C# provides a simple and straightforward way to initialize arrays when declared by enclosing the initial value in braces ({}). The following example shows various methods for initializing arrays of different types.
NoticeIf the array is not initialized at declaration, the array members are automatically initialized to the default initial value of that array type. Also, if an array is declared as a field of a certain type, it will be set to the default value when the type is instantiated null。
One-dimensional array
Copy the codeThe code is as follows:
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
The size of the array can be omitted, as shown below:
Copy the codeThe code is as follows:
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
If an initializer is provided, the new operator can also be omitted, as follows:
Copy the codeThe code is as follows:
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};
Multidimensional array
Copy the codeThe code is as follows:
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
The size of the array can be omitted, as shown below:
Copy the codeThe code is as follows:
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
If an initializer is provided, the new operator can also be omitted, as follows:
Copy the codeThe code is as follows:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
Interleaved array (array of array)
The interleaved array can be initialized as shown in the following example:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
The size of the first array can be omitted, as shown below:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
-or-
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Note that there is no initialization syntax for elements of interleaved arrays.
Accessing array members
Accessing array members can be done directly, similar to accessing array members in C/C++. For example, the following code creates an array named numbers and assigns 5 to the fifth element of the array:
Copy the codeThe code is as follows:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;
The following code declares a multidimensional array and assigns 5 to members located in [1, 1]:
Copy the codeThe code is as follows:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
The following declares a one-dimensional interleaved array that contains two elements. The first element is an array of two integers, and the second element is an array of three integers:
int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}
};
The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:
Copy the codeThe code is as follows:
numbers[0][0] = 58;
numbers[1][1] = 667;
Arrays are objects
In C#, arrays are actually objects. is an abstract base type for all array types. You can use properties that have them and other class members. An example of this usage is to use the Length attribute to get the length of an array. The following code assigns the length of the numbers array (5) to a variable named LengthOfNumbers:
Copy the codeThe code is as follows:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = ;
Classes provide many useful other methods/properties, such as methods for sorting, searching, and copying arrays.
Use foreach for arrays
C# also provides foreach statements. This statement provides a simple and clear way to iterate over elements of an array. For example, the following code creates an array called numbers and loops through the array with a foreach statement:
Copy the codeThe code is as follows:
int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
foreach (int i in numbers)
{
(i);
}
Since there are multi-dimensional arrays, the same method can be used to iterate over elements, for example:
Copy the codeThe code is as follows:
int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers)
{
("{0} ", i);
}
The output of this example is:
9 99 3 33 5 55
However, thanks to multi-dimensional arrays, using nested for loops will give you more control over array elements.