C array to learn related data collation

  • 2020-05-07 20:15:10
  • OfStack

array overview

The C# array is indexed from zero, that is, the array index is indexed from zero. Arrays in C# work like they do in most other popular languages. But there are some differences that should be noted.
When you declare an array, the square brackets ([]) must follow the type, not the identifier. In C#, placing square brackets after identifiers is an illegal syntax.

int[] table; // not int table[];

Another detail is that the size of an array is not a part of its type, whereas in C it is a part of an array type. This allows you to declare an array and assign 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 


declares arrays

C# supports 1-dimensional arrays, multidimensional arrays (rectangular arrays), and arrays of arrays (jagged arrays). The following example shows how to declare different types of arrays:

1-dimensional array:

int [] numbers; Multidimensional array:

string [and] names; Array of arrays (interlaced) :

byte [] [] scores; Declaring arrays (as 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:

1-dimensional array:  

int[] numbers = new int[5];
multidimensional array:
string[,] names = new string[5,4];
Array of the array (interlaced) :
 
byte[][] scores = new byte[5][]; 
for (int x = 0; x < scores.Length; x++) 
{ 
scores[x] = new byte[4]; 
} 

You can also have larger arrays. For example, you can have a 3-dimensional array of rectangles:
 
int[,,] buttons = new int[4,5,3]; 

You can even mix rectangular and staggered arrays. For example, the following code declares a 1-dimensional array of a 3-dimensional array of a 2-dimensional array of type int.

int[][,,][,] numbers;
Initializing an array C# provides a simple and straightforward way to initialize an array at declaration time by enclosing the initial value in braces ({}). The following examples show various ways to initialize different types of arrays.  

Note that if the array is not initialized when declared, the array members are automatically initialized to the default initialization value of the array type. Also, if you declare an array as a field of a type, it will be set to the default value null when the type is instantiated.  

1 d 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 shown below:
 
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 shown below:
 
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} }; 
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} }; 

A jagged array (of arrays)
An interlaced 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 a jagged array.  

accesses 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 a 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 a 5 to the member at [1, 1] :
 
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} }; 
numbers[1, 1] = 5; 

Let's declare a 1-dimensional jagged 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; 


The array is the object
In C#, arrays are actually objects. System.Array is the abstract base type for all array types. You can use the properties System.Array has as well as other class members. An example of this usage is to use the "length" (Length) property to get the length of the 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 = numbers.Length; 

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

USES foreach for arrays

  C# also provides the foreach statement. This statement provides a simple, straightforward way to iterate through the elements of an array. For example, the following code creates an array named numbers and loops through the array with the foreach statement:
 
int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0}; 
foreach (int i in numbers) 
{ 
System.Console.WriteLine(i); 
} 

Because of the multidimensional array, you can use the same method to loop through 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 the example is:

9 99 3 33 5 55
However, because of the multidimensional array, using the nested for loop gives you better control over the array elements.

Related articles: