Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1class Test

2{

3

static void Main() {

4

long longValue = Int64.MaxValue;

5

int intValue = (int) longValue;

6

Console.WriteLine("(int) {0} = {1}", longValue, intValue);

7}

8}

9uses an explicit conversion to convert a long to an int. The output is:

10(int) 9223372036854775807 = -1

11because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions.

128.2.3 Array types

13Arrays can be single-dimensional or multi-dimensional. Both “rectangular” and “jagged” arrays are

14supported.

15Single-dimensional arrays are the most common type. The example

16using System;

17class Test

18{

19

static void Main() {

20

int[] arr = new int[5];

21

for (int i = 0; i < arr.Length; i++)

22

arr[i] = i * i;

23

for (int i = 0; i < arr.Length; i++)

24

Console.WriteLine("arr[{0}] = {1}", i, arr[i]);

25}

26}

27creates a single-dimensional array of int values, initializes the array elements, and then prints each of them

28out. The output produced is:

29arr[0] = 0

30arr[1] = 1

31arr[2] = 4

32arr[3] = 9

33arr[4] = 16

34The type int[] used in the previous example is an array type. Array types are written using a non-array-

35type followed by one or more rank specifiers. The example

36class Test

37{

38

static void Main() {

 

39

int[] a1;

// single-dimensional array of int

40

int[,] a2;

// 2-dimensional array of int

41

int[,,] a3;

// 3-dimensional array of int

42

int[][] j2;

// "jagged" array: array of (array of int)

43

int[][][] j3;

// array of (array of (array of int))

44}

45}

46shows a variety of local variable declarations that use array types with int as the element type.

47Array types are reference types, and so the declaration of an array variable merely sets aside space for the

48reference to the array. Array instances are actually created via array initializers and array creation

49expressions. The example

20

Chapter 8 Language overview

1class Test

2{

3

static void Main() {

 

 

4

int[] a1 = new int[] {1,

2,

3};

5

int[,] a2 = new int[,] {{1,

2, 3}, {4, 5, 6}};

6

int[,,] a3

= new int[10, 20, 30];

7

int[][] j2

= new int[3][];

 

8

j2[0] = new int[] {1, 2, 3};

 

9

j2[1] = new int[] {1, 2, 3, 4, 5, 6};

10

j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

11}

12}

13shows a variety of array creation expressions. The variables a1, a2 and a3 denote rectangular arrays, and

14the variable j2 denotes a jagged array. It should be no surprise that these terms are based on the shapes of

15the arrays. Rectangular arrays always have a rectangular shape. Given the length of each dimension of the

16array, its rectangular shape is clear. For example, the lengths of a3’s three dimensions are 10, 20, and 30,

17respectively, and it is easy to see that this array contains 10*20*30 elements.

18In contrast, the variable j2 denotes a “jagged” array, or an “array of arrays”. Specifically, j2 denotes an

19array of an array of int, or a single-dimensional array of type int[]. Each of these int[] variables can be

20initialized individually, and this allows the array to take on a jagged shape. The example gives each of the

21int[] arrays a different length. Specifically, the length of j2[0] is 3, the length of j2[1] is 6, and the

22length of j2[2] is 9.

23[Note: In C++, an array declared as int x[3][5][7] would be considered a three dimensional rectangular

24array, while in C#, the declaration int[][][] declares a jagged array type. end note]

25The element type and shape of an array—including whether it is jagged or rectangular, and the number of

26dimensions it has—are part of its type. On the other hand, the size of the array—as represented by the length

27of each of its dimensions—is not part of an array’s type. This split is made clear in the language syntax, as

28the length of each dimension is specified in the array creation expression rather than in the array type. For

29instance the declaration

30int[,,] a3 = new int[10, 20, 30];

31has an array type of int[,,] and an array creation expression of new int[10, 20, 30].

32For local variable and field declarations, a shorthand form is permitted so that it is not necessary to re-state

33the array type. For instance, the example

34int[] a1 = new int[] {1, 2, 3};

35can be shortened to

36int[] a1 = {1, 2, 3};

37without any change in program semantics.

38The context in which an array initializer such as {1, 2, 3} is used determines the type of the array being

39initialized. The example

40class Test

41{

42

static void Main() {

43

short[] a = {1, 2, 3};

44

int[] b = {1, 2, 3};

45

long[] c = {1, 2, 3};

46}

47}

48shows that the same array initializer syntax can be used for several different array types. Because context is

49required to determine the type of an array initializer, it is not possible to use an array initializer in an

50expression context without explicitly stating the type of the array.

21

Соседние файлы в предмете Электротехника