How C # One-dimensional arrays are declared
Int[] MyArray;
String[] Mystrarr;
However, it must be initialized before accessing the array.
There are two ways to initialize a C # array, the first of which is to assign an initial value to an array's elements when declaring it:
Int[] MyArray = {5, 9, 15, 22, 30};
String[] Mystrarr = {"Diagram", "League", "Brambling"};
Another way is to specify the size of the array when declaring the array (that is, the length of the array, the number of array elements):
int[] MyArray = new INT[5];
string[] Mystrarr = new String[3];
It is not necessarily a numeric value, but it can also be a variable with a constant (const) keyword:
const int arrsize = 5;
int[] MyArray = new Int[arrsize];
string[] Mystrarr = new String[arrsize];
Access array elements can be labeled with the for Loop and array elements:
Int[] MyArray = {5, 9, 15, 22, 30};
for (int i = 0; i < myarray.length; i++)
{
Console.WriteLine ("Array length {0}, {1} elements are: {2}", Myarray.length, I, myarray[i]);
}
Console.readkey ();
You can also use foreach to access each element of an array, but the Foreach loop has read-only access to the contents of arrays, so you cannot change the value of any element:
Int[] MyArray = {5, 9, 15, 22, 30};
foreach (int num in myArray)
{
Console.WriteLine ("array element: {0}", num);
}
Console.readkey ();
Two-dimensional arrays:
The simplest way to declare a two-dimensional array:
int[,] myintarray;
string[,] mystrarray;
A 4-D array is declared as follows:
int[,,,] Myintarray;
Multidimensional arrays are declared only by adding multiple commas on top of a two-dimensional array.
How to initialize a two-dimensional array:
int[,] Myintarray = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
string[,] Mystrarray = new string[3, 2];
A two-dimensional array accesses an array element as a subscript:
myintarray[1,2]; 8
Myintarray is a two-dimensional array of 3 rows and 3 columns, so the 1 in myintarray[1,2] refers to the second nested array of arrays Myintarray (the ID under the array starts at 0, so 1 is the second), myintarray[1,2] 2 refers to the third element in the second nested array of Myintarray, so its value is 8.
Multidimensional Arrays:
Two types of declaration and initialization of multidimensional arrays, the first of which:
int[][] myintarrays = new int[2][];
Myintarrays[0] = new INT[3];
MYINTARRAYS[1] = new INT[4];
Another way:
Int[][] Myintarrays = {new Int[]{1,2,3},new int[]{2,3,4,5}};
The elements of a multidimensional array can be accessed with a foreach loop:
Int[][] Myintarrays = {new Int[]{1,2,3},new int[]{2,3,4,5}};
foreach (int[] Numarr in myintarrays)
{
foreach (int num in Numarr)
{
Console.WriteLine ("array element: {0}", num);
}
}
The output is:
Array elements are: 1
Array elements are: 2
Array elements are: 3
Array elements are: 2
Array elements are: 3
Array elements are: 4
Array elements are: 5
This allows you to see the order in which multidimensional array elements are accessed.
Blog for the first time, the blog is open in recent days, only because they are too much food, so want to record the usual learning some things. I would be honored if I could help others!
Declaration of an array of C #