Книга: C# 2008 Programmer
Arrays
Arrays
An array is a data structure containing several variables of the same type. For example, you might have an array of integer values, like this:
int[] nums;
In this case, nums
is an array that has yet to contain any elements (of type int
). To make nums an array containing 10 elements, you can instantiate it with the new keyword followed by the type name and then the size of the array:
nums = new int[10];
The index for each element in the array starts from 0
and ends at n-1
(where n is the size of the array). To assign a value to each element of the array, you can specify its index as follows:
nums[0] = 0;
nums[1] = 1;
//...
nums[9] = 9;
Arrays are reference types, but array elements can be of any type.
Instead of assigning values to each element in an array individually, you can combine them into one statement, like this:
int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Arrays can be single-dimensional (which is what you have seen so far), multi-dimensional, or jagged. You'll find more about arrays in Chapter 13, in the discussion of collections.