Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

ENTER SINGLE-DIMENSIONAL ARRAYS

Single-dimensional arrays let you define a set of variables and store them in one block of memory for easy retrieval. C# single-dimensional arrays can

include defined sets of data. For example, you can enter a set of numbers or a set of string values such as the number of months in a year. You can use any value type as part of your array including integral, floating point, decimal, Boolean, struct, and enumeration.

After you declare your array, you must initialize it by using the new operator. When you initialize the array, you can give the array specific values such as numbers or specify the maximum number of elements in the array.

You give the array specific values as you do in C, C++, and Java — placing the values in curly braces at the end of the array argument. If you specify the maximum number of elements in an array instead, C# automatically assigns the first element in your array the number zero. For example, an array with six elements will be numbered 0 through 5.

Accessing an array in C# is very similar to what you find in C and C++. For example, you can create an integer array, then assign an integer to a particular location in that array.

ENTER SINGLE-DIMENSIONAL ARRAYS

Properties

page appears. The New Project window

appears.

Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

You can omit optional parts of the single-dimensional array argument. One way is to omit the size of the array.

int[] values = new int[] {1, 2, 3, 5, 7, 11};

string[] letters = new string[] {"A", "B", "C"};

Another way is to omit the new statement altogether.

int[] values = {1, 2, 3, 5, 7, 11};

string[] letters = {"A", "B", "C"};

Á Type the code that outputs the opening string and establishes the array in the Main method.

Type the code that outputs the array by iterating through each element using the foreach statement.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The prime number array

 

appears on the screen.

 

139

C#

ADD MULTIDIMENSIONAL ARRAYS

C# lets you declare multidimensional arrays for processing a large number of values in one argument. A multidimensional array arranges its data

similar to the way a spreadsheet does.

C# multidimensional arrays let you specify two or three elements in the array for two-dimensional and threedimensional arrays, respectively. You can use twodimensional arrays for specifying coordinates such as with the row and column in a spreadsheet, on a map, or on a game board such as those for checkers and chess. Programmers use two-dimensional arrays for such tasks as image processing.

A three-dimensional array lets you specify three elements. For example, you can store a name in three dimensions — first name, middle name, and last name.

Just as with single-dimensional arrays, you can specify the number of elements in each dimension in the rectangular brackets after you declare the array type. If you think of the array as the table, C# lists the number of rows first and the number of columns second. If you have a three-dimensional array, then the third dimension appears last in the bracket.

You can also specify initial values for the array in the same order as you have them in the rectangular brackets. Like single-dimensional arrays, values appear in curly braces after you initialize the array with the new operator.

ADD MULTIDIMENSIONAL ARRAYS

Properties

page appears. The New Project window

appears.

New Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

C# contains rules about array structure that you must adhere to so your array can function properly. The rules include:

Specify the size of your dimensions when you create a multidimensional array. If you have a particular array with the value x, you can specify the size of x-1 dimensions, but it is usually safer, not to mention less confusing, if you specify the information up front.

When you create the array, it is a good idea to keep the same dimensions for every other array in your program. This approach can reduce confusion for users of your program.

Microsoft recommends that you use the first dimension as the row and the second as the column, but you can define your dimension order however you like. Your dimension order must be consistent throughout your program.

Á Type the code that outputs the array.

Type the Main method that establishes the array.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

Type information at the

 

prompts and the output

 

appears.

 

141

Соседние файлы в папке c#