Arrays in C#
Def: An array is a data structure that contains a number of elements of the same type.
Desc: An array is declared by defining the type of elements inside the array followed by empty brackets and a variable name. For example, int[] myArray;
After declaring an array, memory must be allocated to hold all the elements of the array. An array is a reference type, so memory on the heap must be allocated. You do this by initializing the variable of the array using the new operator with the type and the number of elements inside the array. Here you specify the size of the array:
myArray = new int[4];
The array cannot be resized after the size was specifi ed without copying all elements.
To declare and initialize an array, you can use a single line:
int[] myArray = new int[4]; or int[] myArray = new int[] {4, 7, 11, 2};
Using curly brackets you can write the array declaration and initialization shorter:
int[] myArray = {4, 7, 11, 2};
After an array is declared and initialized, you can access the array elements using an indexer. With the indexer, you pass the element number to access the array. The indexer starts with 0 and the highest number is the number of elements minus one. example,
int[] myArray = new int[] {4, 7, 11, 2};
int v1 = myArray[0]; // read first element
int v2 = myArray[1]; // read second element
myArray[3] = 44; // change fourth element
Дата добавления: 2015-09-18 | Просмотры: 484 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|