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

Chapter 12 Variables

112. Variables

2Variables represent storage locations. Every variable has a type that determines what values can be stored in

3the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are

4always of the appropriate type. The value of a variable can be changed through assignment or through use of

5the ++ and -- operators.

6A variable shall be definitely assigned (§12.3) before its value can be obtained.

7As described in the following subclauses, variables are either initially assigned or initially unassigned. An

8initially assigned variable has a well-defined initial value and is always considered definitely assigned. An

9initially unassigned variable has no initial value. For an initially unassigned variable to be considered

10definitely assigned at a certain location, an assignment to the variable shall occur in every possible execution

11path leading to that location.

1212.1 Variable categories

13C# defines seven categories of variables: static variables, instance variables, array elements, value

14parameters, reference parameters, output parameters, and local variables. The subclauses that follow

15describe each of these categories.

16[Example: In the following code

17class A

18{

19

public static int x;

20

int y;

21

void F(int[] v, int a, ref int b, out int c) {

22

int i = 1;

23

c = a + b++;

24}

25}

26x is a static variable, y is an instance variable, v[0] is an array element, a is a value parameter, b is a

27reference parameter, c is an output parameter, and i is a local variable. end example]

2812.1.1 Static variables

29A field declared with the static modifier is called a static variable. A static variable comes into existence

30before execution of the static constructor (§17.11) for its containing type, and ceases to exist when the

31associated application domain ceases to exist.

32The initial value of a static variable is the default value (§12.2) of the variable’s type.

33For the purposes of definite assignment checking, a static variable is considered initially assigned.

3412.1.2 Instance variables

35A field declared without the static modifier is called an instance variable.

3612.1.2.1 Instance variables in classes

37An instance variable of a class comes into existence when a new instance of that class is created, and ceases

38to exist when there are no references to that instance and the instance’s destructor (if any) has executed.

39The initial value of an instance variable of a class is the default value (§12.2) of the variable’s type.

40For the purpose of definite assignment checking, an instance variable is considered initially assigned.

115

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