Arrays in C#

高洛峰
Release: 2016-12-16 14:14:16
Original
1295 people have browsed it

Array Overview
An array is a data structure that contains several variables of the same type. Arrays are declared using types:
Arrays have the following properties:

Arrays can be one-dimensional, multi-dimensional, or interleaved.

The default value of numeric array elements is set to zero, while the default value of reference elements is set to null.

A jagged array is an array of arrays, therefore, its elements are reference types, initialized to null.

Arrays are indexed from zero: an array with n elements is indexed from 0 to n-1.

Array elements can be of any type, including array types.

Array types are reference types derived from the abstract base type Array. Because this type implements IEnumerable and IEnumerable, you can use foreach iteration on all arrays in C#.

C# arrays are indexed from zero, that is, the array index starts from zero. Arrays work in C# similarly to how they work in most other popular languages. But there are some differences that should be noted.
When declaring an array, square brackets ([]) must follow the type, not the identifier. In C#, it is illegal syntax to put square brackets after an identifier.

int[] table; // not int table[];
Copy after login

Another detail is that the size of an array is not part of its type, you can declare an array and assign to it an arbitrary 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
Copy after login

Declare arrays
C# supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays) and arrays of arrays (interleaved arrays). The following example shows how to declare different types of arrays:
One-dimensional array:

int[] numbers;
Copy after login

Multi-dimensional array:

string[,] names;
Copy after login

Array of arrays (interleaved):

byte[][] scores;
Copy after login

Declaring arrays (shown above) does not actually create them.

In C#, arrays are objects and must be instantiated. The following example shows how to create an array:
One-dimensional array:

int[] numbers = new int[5];
Copy after login

Multi-dimensional array:

string[,] names = new string[5,4];
Copy after login

Array of arrays (interleaved):

byte[][] scores = new byte[5][];
for (int x = 0; x < scores.Length; x++) 
{
   scores[x] = new byte[4];
}
Copy after login

Larger arrays are also possible. For example, you can have three-dimensional rectangular arrays:

int[,,] buttons = new int[4,5,3];
Copy after login

You can even mix rectangular arrays and jagged arrays. For example, the following code declares a one-dimensional array of a two-dimensional array of type int and a three-dimensional array of types.

int[][,,][,] numbers;
Copy after login

Initializing Arrays
Provides a simple and straightforward way to initialize an array at declaration time by enclosing the initial value within curly braces ({}). The following examples show various ways to initialize arrays of different types.
Note If the array is not initialized when declared, the array members will automatically be initialized to the default initial value of the array type. Additionally, if an array is declared as a field of a type, it will be set to the default value of null when the type is instantiated.
One-dimensional array

int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
Copy after login

The size of the array can be omitted, as follows:

int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
Copy after login

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"};
Copy after login

Multidimensional arrays

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
Copy after login

Can Omitting the size of the array, as shown below:

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
Copy after login

If an initializer is provided, the new operator can also be omitted, as shown below:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
Copy after login

A jagged array (array of arrays)
can be used as in the following example Initialize a jagged array like this:

int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Copy after login

You can omit the size of the first array, like this:

int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Copy after login

- or -

int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
Copy after login

Note that there is no initialization syntax for the elements of a jagged array.

Accessing array members
Accessing array members can be done directly. For example, the following code creates an array named numbers, and then 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;
Copy after login

The following code declares a multidimensional array, and assign 5 to the member located at [1, 1]:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
Copy after login

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}};
Copy after login

The following statement assigns 58 to the first element of the first array and to the second element of the second array. elements are assigned 667:

numbers[0][0] = 58;
numbers[1][1] = 667;
Copy after login

Arrays are objects In C#, arrays are actually objects. System.Array is the abstract base type for all array types. You can use the properties that System.Array has as well as other class members. An example of this usage is to use the Length property to get the length of an array. The following code assigns the length of the numbers array (which is 5) to a variable called LengthOfNumbers:

int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;
Copy after login

The System.Array class provides many useful other methods/properties such as methods for sorting, searching, and copying arrays.

Use foreach on arrays
C# also provides the foreach statement. This statement provides a simple, clear way to iterate through the elements of an array.
For example, the following code creates an array called numbers and iterates over the array using a foreach statement:

int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
foreach (int i in numbers)
{
   System.Console.WriteLine(i);
}
Copy after login

Thanks to multidimensional arrays, the same method can be used to iterate over the elements, for example:

int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers)
{
   Console.Write("{0} ", i);
}
Copy after login

The output of this example is :

9 99 3 33 5 55
Copy after login

However, thanks to multidimensional arrays, using nested for loops will give you greater control over the array elements.


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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!