Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 14 ARRAYS

Types of Arrays

C# provides two kinds of arrays:

One-dimensional arrays can be thought of as a single line, or vector, of elements.

Multidimensional arrays are composed such that each position in the primary vector is itself an array, called a subarray. Positions in the subarray vectors can themselves be subarrays.

Additionally, there are two types of multidimensional arrays, rectangular arrays and jagged arrays, which have the following characteristics:

Rectangular arrays

Are multidimensional arrays where all the subarrays in a particular dimension have the same length

Always use a single set of square brackets, regardless of the number of dimensions

int x = myArray2[4, 6, 1]

// One set of square brackets

Jagged arrays

Are multidimensional arrays where each subarray is an independent array

Can have subarrays of different lengths

Use a separate set of square brackets for each dimension of the array

jagArray1[2][7][4] // Three sets of square brackets

Figure 14-2 shows the kinds of arrays available in C#.

Figure 14-2. One-dimensional, rectangular, and jagged arrays

343

CHAPTER 14 ARRAYS

An Array As an Object

An array instance is an object whose type derives from class System.Array. Since arrays are derived from this BCL base class, they inherit a number of useful members from it, such as the following:

Rank: A property that returns the number of dimensions of the array

Length: A property that returns the length (the total number of elements)of the array

Arrays are reference types, and as with all reference types, they have both a reference to the data and the data object itself. The reference is in either the stack or the heap, and the data object itself will always be in the heap. Figure 14-3 shows the memory configuration and components of an array.

Figure 14-3. Structure of an array

Although an array is always a reference type, the elements of the array can be either value types or reference types.

An array is called a value type array if the elements stored are value types.

An array is called a reference type array if the elements stored in the array are references of reference type objects.

Figure 14-4 shows a value type array and a reference type array.

Figure 14-4. Elements can be values or references.

344

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]