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

Chapter 17 Classes

1class Test

2{

3

static void Main() {

4

Console.WriteLine("{0} {1}", B.Y, A.X);

5

}

6

public static int f(string s) {

7

Console.WriteLine(s);

8

return 1;

9}

10}

11class A

12{

13

static A() {}

14public static int X = Test.f("Init A");

15}

16class B

17{

18

static B() {}

19public static int Y = Test.f("Init B");

20}

21the output shall be:

22Init B

23Init A

241 1

25because the rules for when static constructors execute provide that B's static constructor (and hence B's static

26field initializers) shall run before A's static constructor and field initializers. end example]

2717.4.5.2 Instance field initialization

28The instance field variable initializers of a class correspond to a sequence of assignments that are executed

29immediately upon entry to any one of the instance constructors (§17.10.2) of that class. The variable

30initializers are executed in the textual order in which they appear in the class declaration. The class instance

31creation and initialization process is described further in §17.10.

32A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-

33time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to

34reference any instance member through a simple-name. [Example: In the following code

35class A

36{

37

int x = 1;

38int y = x + 1; // Error, reference to instance member of this

39}

40the variable initializer for y results in a compile-time error because it references a member of the instance

41being created. end example]

4217.5 Methods

43A method is a member that implements a computation or action that can be performed by an object or class.

44Methods are declared using method-declarations:

45method-declaration:

46

method-header method-body

47

method-header:

48

attributesopt method-modifiersopt return-type member-name type-parameter-listopt

49

( formal-parameter-listopt ) type-parameter-constraints-clausesopt

50

method-modifiers:

51

method-modifier

52

method-modifiers method-modifier

277

 

C# LANGUAGE SPECIFICATION

1

method-modifier:

2

new

3

public

4

protected

5

internal

6

private

7

static

8

virtual

9

sealed

10

override

11

abstract

12extern

13return-type:

14

type

15void

16member-name:

17

identifier

18

interface-type . identifier

19

method-body:

20

block

21;

22The member-name specifies the name of the method. Unless the method is an explicit interface member

23implementation (§20.4.1), the member-name is simply an identifier. For an explicit interface member

24implementation, the member-name consists of an interface-type followed by a “.” and an identifier.

25A method-declaration can include a set of attributes (§24) and a valid combination of the four access

26modifiers (§17.2.3), the new (§17.2.2), static (§17.5.2), virtual (§17.5.3), override (§17.5.4),

27sealed (§17.5.5), abstract (§17.5.6), and extern (§17.5.7) modifiers.

28A declaration has a valid combination of modifiers if all of the following are true:

29The declaration includes a valid combination of access modifiers (§17.2.3).

30The declaration does not include the same modifier multiple times.

31The declaration includes at most one of the following modifiers: static, virtual, and override.

32The declaration includes at most one of the following modifiers: new and override.

33If the declaration includes the abstract modifier, then the declaration does not include any of the

34following modifiers: static, virtual, sealed, or extern.

35If the declaration includes the private modifier, then the declaration does not include any of the

36following modifiers: virtual, override, or abstract.

37If the declaration includes the sealed modifier, then the declaration also includes the override

38modifier.

39If the declaration is an explicit interface member implementation (indicated by a member-name

40including an interface-type, §20.4.1), then the declaration includes no modifiers other than possibly the

41extern modifier.

42The return-type of a method declaration specifies the type of the value computed and returned by the

43method. The return-type is void if the method does not return a value.

44The optional type-parameter-list specifies the type parameters for a generic method. The optional type-

45parameter-constraints-clauses specify the constraints for the type parameters. A method-declaration shall

46not have type-parameter-constraints-clauses unless it also has a type-parameter-list. A method-declaration

47for an explicit interface member implementation shall not have any type-parameter-constraints-clauses. A

278

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