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

Chapter 17 Classes

1T get_P();

2void set_P(T value);

3Both signatures are reserved, even if the property is read-only or write-only.

4[Example: In the following code

5using System;

6class A

7{

8

public

int P {

9

get

{ return 123; }

10}

11}

12class B: A

13{

14

new public int get_P() {

15

return 456;

16

}

17

new public void set_P(int value) {

18}

19}

20class Test

21{

22

static void Main() {

23

B b = new B();

24

A a = b;

25

Console.WriteLine(a.P);

26

Console.WriteLine(b.P);

27

Console.WriteLine(b.get_P());

28}

29}

30a class A defines a read-only property P, thus reserving signatures for get_P and set_P methods. A class B

31derives from A and hides both of these reserved signatures. The example produces the output:

32123

33123

34456

35end example]

3617.2.7.2 Member names reserved for events

37For an event E (§17.7) of delegate type T, the following signatures are reserved:

38void add_E(T handler);

39void remove_E(T handler);

4017.2.7.3 Member names reserved for indexers

41For an indexer (§17.8) of type T with parameter-list L, the following signatures are reserved:

42T get_Item(L);

43void set_Item(L, T value);

44Both signatures are reserved, even if the indexer is read-only or write-only.

4517.2.7.4 Member names reserved for destructors

46For a class containing a destructor (§17.12), the following signature is reserved:

47void Finalize();

4817.3 Constants

49A constant is a class member that represents a constant value: a value that can be computed at compile-time.

50A constant-declaration introduces one or more constants of a given type.

269

 

C# LANGUAGE SPECIFICATION

1

constant-declaration:

2

attributesopt constant-modifiersopt const type constant-declarators ;

3

constant-modifiers:

4

constant-modifier

5

constant-modifiers constant-modifier

6

constant-modifier:

7

new

8

public

9

protected

10

internal

11private

12constant-declarators:

13

constant-declarator

14

constant-declarators , constant-declarator

15

constant-declarator:

16

identifier = constant-expression

17A constant-declaration can include a set of attributes (§24), a new modifier (§17.2.2), and a valid

18combination of the four access modifiers (§17.2.3). The attributes and modifiers apply to all of the members

19declared by the constant-declaration. Even though constants are considered static members, a constant-

20declaration neither requires nor allows a static modifier. It is an error for the same modifier to appear

21multiple times in a constant declaration.

22The type of a constant-declaration specifies the type of the members introduced by the declaration. The type

23is followed by a list of constant-declarators, each of which introduces a new member. A constant-declarator

24consists of an identifier that names the member, followed by an “=” token, followed by a constant-

25expression (§14.15) that gives the value of the member.

26The type specified in a constant declaration shall be sbyte, byte, short, ushort, int, uint, long,

27ulong, char, float, double, decimal, bool, string, an enum-type, or a reference-type. Each constant-

28expression shall yield a value of the target type or of a type that can be converted to the target type by an

29implicit conversion (§13.1).

30The type of a constant shall be at least as accessible as the constant itself (§10.5.4).

31The value of a constant is obtained in an expression using a simple-name (§14.5.2) or a member-access

32(§14.5.4).

33A constant can itself participate in a constant-expression. Thus, a constant can be used in any construct that

34requires a constant-expression. [Note: Examples of such constructs include case labels, goto case

35statements, enum member declarations, attributes, and other constant declarations. end note]

36[Note: As described in §14.15, a constant-expression is an expression that can be fully evaluated at compile-

37time. Since the only way to create a non-null value of a reference-type other than string is to apply the

38new operator, and since the new operator is not permitted in a constant-expression, the only possible value

39for constants of reference-types other than string is null. end note]

40When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a

41constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a

42readonly field (§17.4.2) can be used instead. [Note: The versioning semantics of const and readonly

43differ (§17.4.2.2). end-note]

44A constant declaration that declares multiple constants is equivalent to multiple declarations of single

45constants with the same attributes, modifiers, and type. [Example:

46class A

47{

48public const double X = 1.0, Y = 2.0, Z = 3.0;

49}

270

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