Home >Backend Development >C#.Net Tutorial >How to define an array in C#
One, one dimension:
int[] numbers = new int[]{1,2,3,4,5,6}; //Indefinite length
int[] numbers = new int[3]{1,2, 3};//Fixed length
Second, multi-dimensional
int[,] numbers = new int[,]{{1,2,3},{1,2,3}}; //Unfixed length
int[,] numbers = new int[2,2]{{1,2},{1,2}}; // Fixed length
3. Example
A: int[] mf1=new int[6];
Initialize the range of the array, or specify the initial value; //One-dimensional integer array containing 6 elements, initial value 1, 2, 3, 4, 5, 6 int[] mf2=new int[6]{1,2 ,3,4,5,6};
B://One-dimensional string array, if an initializer is provided, the new operator can also be omitted
string[] mf3={"c"," c++","c#"};
C://One-dimensional object array
Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 };
D://Two-dimensional integer Array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4 int[,] mf5=new int[, ]{{1,2},{3,4}};
E: //6*6 two-dimensional integer array
int[,] mf6=new mf[6,6];
4. Obtain Number of array elements:
int b;
b = sizeof (a)/sizeof (*a); Time)
string time=convert.tostring(DateTime.Today).split( new char []{' '}); textbox1.text=time[0]; Use spaces as dividing points;
C# array from Zero-based indexing means that array indexing 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[];
Another detail is that the size of an array is not part of its type, whereas in C language it is part of the array type. This allows you to 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
Declare array
C# supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays) and arrays of arrays (interleaved arrays). The following examples show how to declare arrays of different types:
One-dimensional array: int[] numbers;
Multi-dimensional array: string[,] names;
Array of arrays (interleaved): byte[][] scores;
Declaring an array (shown above) does not actually create them. In C#, arrays are objects (discussed later in this tutorial) and must be instantiated. The following example shows how to create an array:
One-dimensional array: int[] numbers = new int[5];
Multi-dimensional array: string[,] names = new string[5,4];
}
You can also have Larger array. For example, you can have a three-dimensional rectangular array: int[,,] buttons = new int[4,5,3];
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 a three-dimensional array of type int[][,,][,] numbers;
Initialize an array
C# by enclosing the initial value in curly braces ({} ) provides a simple and straightforward way to initialize an array at declaration time. 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 "};
The size of the array can be omitted, as follows:
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};
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 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:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
staggered array (array of arrays)
staggered 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 first number can be omitted The size of an array is as follows:
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} };
Please note that for There is no initialization syntax for the elements of a jagged array.
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 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;
The following code declares a multidimensional array and assigns 5 to the member located at [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, which 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. 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 named LengthOfNumbers:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;
System.Array The class provides many useful additional 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 named numbers and iterates through 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);}
Thanks to multi-dimensional arrays, you can use the same method to iterate over elements, for example:
int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers){Console.Write("{0} ", i);}
The output of this example is: 9 99 3 33 5 55
However, due to multi-dimensional arrays, using nested for loops will allow you to better control the array elements
For more articles related to how to define arrays in C#, please pay attention to the PHP Chinese website!