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

 

Chapter 17 Classes

1

public void SubmitOrder(Order orderSubmitted) {

2

orders.Add(orderSubmitted);

3

}

4

public bool HasOutstandingOrders() {

5

return orders.Count > 0;

6}

7}

8end example]

9The handling of attributes specified on the type or type parameters of different parts of a partial declaration

10is discussed in §24.2

1117.2 Class members

12The members of a class consist of the members introduced by its class-member-declarations and the

13members inherited from the direct base class.

14class-member-declarations:

15

16

17

18

19

20

21

22

23

24

25

26

27

28

class-member-declaration

class-member-declarations class-member-declaration

class-member-declaration: constant-declaration field-declaration method-declaration property-declaration event-declaration indexer-declaration operator-declaration constructor-declaration destructor-declaration static-constructor-declaration type-declaration

29The members of a class are divided into the following categories:

30Constants, which represent constant values associated with that class (§17.3).

31Fields, which are the variables of that class (§17.4).

32Methods, both non-generic and generic, which implement the computations and actions that can be

33performed by that class (§17.5, §26.6).

34Properties, which define named characteristics and the actions associated with reading and writing those

35characteristics (§17.6).

36Events, which define notifications that can be generated by that class (§17.7).

37Indexers, which permit instances of that class to be indexed in the same way as arrays (§17.8).

38Operators, which define the expression operators that can be applied to instances of that class (§17.9).

39Instance constructors, which implement the actions required to initialize instances of that class (§17.10)

40Destructors, which implement the actions to be performed before instances of that class are permanently

41discarded (§17.12).

42Static constructors, which implement the actions required to initialize that class itself (§17.11).

43Types, which represent the types that are local to that class (§16.6).

44Members that can contain executable code are collectively known as the function members of the class. The

45function members of a class are the methods, properties, events, indexers, operators, instance constructors,

46destructors, and static constructors of that class.

261

C# LANGUAGE SPECIFICATION

1A class-declaration creates a new declaration space (§10.3), and the type-parameters and the class-member-

2declarations immediately contained by the class-declaration introduce new members into this declaration

3space. The following rules apply to class-member-declarations:

4Instance constructors, destructors, and static constructors shall have the same name as the immediately

5enclosing class. All other members shall have names that differ from the name of the immediately

6enclosing class.

7The name of a type parameter in the type-parameter-list of a class declaration shall differ from the

8names of all other type parameters in the same type-parameter-list and shall differ from the name of the

9class and the names of all members of the class.

10The name of a type must differ from the names of all non-type members declared in the same class. If

11two or more type declarations share the same fully qualified name, the declarations must have the partial

12modifier (§17.1.4) and these declarations combine to define a single type. [Note: Since the fully

13qualified name of a type declaration encodes the number of type parameters, two distinct types may

14share the same name as long as they have different number of type parameters. end note]

15The name of a constant, field, property, or event shall differ from the names of all other members

16declared in the same class.

17The name of a method shall differ from the names of all other non-methods declared in the same class.

18In addition, the signature (§10.6) of a method shall differ from the signatures of all other methods

19declared in the same class, and two methods declared in the same class shall not have signatures that

20differ solely by ref and out.

21The signature of an instance constructor shall differ from the signatures of all other instance constructors

22declared in the same class, and two constructors declared in the same class shall not have signatures that

23differ solely by ref and out.

24The signature of an indexer shall differ from the signatures of all other indexers declared in the same

25class.

26The signature of an operator shall differ from the signatures of all other operators declared in the same

27class.

28The inherited members of a class (§17.2.1) are not part of the declaration space of a class. [Note: Thus, a

29derived class is allowed to declare a member with the same name or signature as an inherited member

30(which in effect hides the inherited member). end note]

31The set of members of a type declared in multiple parts (§17.1.4) is the union of the members declared in

32each part. The bodies of all parts of the type declaration share the same declaration space (§10.3), and the

33scope of each member (§10.7) extends to the bodies of all the parts. The accessibility domain of any member

34always includes all the parts of the enclosing type; a private member declared in one part is freely

35accessible from another part. It is a compile-time error to declare the same member in more than one part of

36the type, unless that member is a type having the partial modifier. [Example:

37partial class A

38{

39

int x;

// Error, cannot declare x more than once

40

partial class Inner

// Ok, Inner is a partial type

41

{

 

42

int y;

 

43}

44}

45partial class A

46{

47

int x;

// Error, cannot declare x more than once

262

 

Chapter 17 Classes

 

1

partial class Inner

// Ok, Inner is a partial type

2

{

 

3

int z;

 

4}

5}

6end example]

7The ordering of members within a type declared in multiple parts is undefined. [Note: Although the ordering

8of members within a type is not significant to C# code, it may be significant when interfacing with other

9languages and environments. end note]

1017.2.1 Inheritance

11A class inherits the members of its direct base class. Inheritance means that a class implicitly contains all

12members of its direct base class, except for the instance constructors, destructors, and static constructors of

13the base class. Some important aspects of inheritance are:

14Inheritance is transitive. If C is derived from B, and B is derived from A, then C inherits the members

15declared in A as well as the members declared in B.

16A derived class extends its direct base class. A derived class can add new members to those it inherits,

17but it cannot remove the definition of an inherited member.

18Instance constructors, destructors, and static constructors are not inherited, but all other members are,

19regardless of their declared accessibility (§10.5). However, depending on their declared accessibility,

20inherited members might not be accessible in a derived class.

21A derived class can hide (§10.7.1.2) inherited members by declaring new members with the same name

22or signature. However, hiding an inherited member does not remove that member—it merely means that

23member will not be found by member lookup within the derived class.

24An instance of a class contains a set of all instance fields declared in the class and its base classes, and

25an implicit conversion (§13.1.4) exists from a derived class type to any of its base class types. Thus, a

26reference to an instance of some derived class can be treated as a reference to an instance of any of its

27base classes.

28A class can declare virtual methods, properties, indexers, and events, and derived classes can override

29the implementation of these function members. This enables classes to exhibit polymorphic behavior

30wherein the actions performed by a function member invocation varies depending on the run-time type

31of the instance through which that function member is invoked.

32Members inherited from a constructed generic type are inherited after type substitution. That is, any

33constituent types in the member have the base class declaration’s type parameters replaced with the

34corresponding type arguments used in the class-base specification.

35[Example: In the example:

36class A<S>

37{

38public S field;

39}

40class B<T> : A<T[]>

41{

42

// inherited: public T[] x;

43public A<T> Meth(T t) { … }

44}

45class C : B<string>

46{

47

// inherited: public string[] x;

48// inherited: public A<string> Meth(string t);

49}

263

C# LANGUAGE SPECIFICATION

1class B<T> inherits member field from A<S> after type parameter S is replaced with the type argument

2T[]. Similarly class C inherits the members of B<T> (including the inherited member field) after type

3parameter T is replaced with the type argument string. end example]

417.2.2 The new modifier

5A class-member-declaration is permitted to declare a member with the same name or signature as an

6inherited member. When this occurs, the derived class member is said to hide the base class member. See

7§10.7.1.2 for a precise specification of when a member hides an inherited member.

8An inherited member M is considered to be available if M is accessible and there is no other inherited

9accessible member N that already hides M.

10Hiding an available inherited member is not considered an error, but it does cause the compiler to issue a

11warning. To suppress the warning, the declaration of the derived class member can include a new modifier

12to indicate that the derived member is intended to hide the base member. If one or more parts of a partial

13declaration §17.1.4) of a nested type include the new modifier, no warning is issued if the nested type hides

14an available inherited member.

15If a new modifier is included in a declaration that doesn’t hide an available inherited member, a warning to

16that effect is issued.

1717.2.3 Access modifiers

18A class-member-declaration can have any one of the five possible kinds of declared accessibility (§10.5.1):

19public, protected internal, protected, internal, or private. Except for the protected

20internal combination, it is a compile-time error to specify more than one access modifier. When a class-

21member-declaration does not include any access modifiers, private is assumed.

2217.2.4 Constituent types

23Types that are used in the declaration of a member are called the constituent types of that member. Possible

24constituent types are the type of a constant, field, property, event, or indexer, the return type of a method or

25operator, and the parameter types of a method, indexer, operator, or instance constructor. The constituent

26types of a member shall be at least as accessible as that member itself (§10.5.4).

2717.2.5 Static and instance members

28Members of a class are either static members or instance members. [Note: Generally speaking, it is useful

29to think of static members as belonging to classes and instance members as belonging to objects (instances

30of classes). end note]

31When a field, method, property, event, operator, or constructor declaration includes a static modifier, it

32declares a static member. In addition, a constant or type declaration implicitly declares a static member.

33Static members have the following characteristics:

34When a static member is referenced in a member-access (§14.5.4) of the form E.M, E shall denote a type

35that has a member M. It is a compile-time error for E to denote an instance.

36A static field in a non-generic class identifies exactly one storage location. No matter how many

37instances of a non-generic class are created, there is only ever one copy of a static field. Each distinct

38closed constructed type (§26.5.2) has its own set of static fields, regardless of the number of instances of

39the closed constructed type.

40A static function member (method, property, event, operator, or constructor) does not operate on a

41specific instance, and it is a compile-time error to refer to this in such a function member.

42When a field, method, property, event, indexer, constructor, or destructor declaration does not include a

43static modifier, it declares an instance member. (An instance member is sometimes called a non-static

44member.) Instance members have the following characteristics:

264

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