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

Chapter 17 Classes

1A static class declaration is subject to the following restrictions:

2A static class shall not include a sealed or abstract modifier. (However, since a static class cannot

3be instantiated or derived from, it behaves as if it were both sealed and abstract.)

4A static class shall not include a class-base specification (§17.1.2) and cannot explicitly specify a base

5class or a list of implemented interfaces. A static class implicitly inherits from type object.

6A static class shall not contain any operators.

7A static class shall not have members with protected or protected internal declared

8accessibility.

9A static class shall only contain static members (§17.2.5). [Note: constants and nested types are

10classified as static members. end note]

11[Example:

12using System.Diagnostics;

13public static class Precondition

14{

15

public sealed class Exception: System.ApplicationException

16

{

17

public Exception(string message) : base(message) {}

18

}

19

[Conditional("CHECK_PRECONDITIONS")]

20

public static void Required(bool condition, string description) {

21

if (!condition) {

22

throw new Exception(description);

23

}

24}

25}

26end example]

27A static class has no instance constructors. It is not possible to declare an instance constructor in a static

28class, and no default instance constructor (§17.10.4) is provided for a static class.

29The members of a static class are not implicitly made static; except for constants and nested types, member

30declarations that are intended to be static shall explicitly include a static modifier. When a class is nested

31within a static outer class, the nested class is not a static class unless it explicitly includes a static

32modifier.

33A namespace-or-type-name (§10.8) is permitted to reference a static class if

34The namespace-or-type-name is the T in a namespace-or-type-name of the form T.I, or

35The namespace-or-type-name is the T in a typeof-expression (§14.5.11) of the form typeof(T).

36A primary-expression (§14.5) is permitted to reference a static class if

37The primary-expression is the E in a member-access (§14.5.4) of the form E.I.

38In any other context it is a compile-time error to reference a static class. [Note: For example, it is an error for

39a static class to be used as a base class, a constituent type (§17.2.4) of a member, a generic type argument, or

40a type parameter constraint. Likewise, a static class cannot be used in an array type, a pointer type, a new

41expression, a cast expression, an is expression, an as expression, a sizeof expression, or a default value

42expression. end note]

43If one or more parts of a partial type declaration (§17.1.4) of a class include the static modifier, the class

44is static. Otherwise, the class is not static.

4517.1.2 Class base specification

46A class declaration can include a class-base specification, which defines the direct base class of the class

47and the interfaces (§20) implemented by the class.

257

 

C# LANGUAGE SPECIFICATION

1

class-base:

2

: class-type

3

: interface-type-list

4

: class-type , interface-type-list

5

interface-type-list:

6

interface-type

7

interface-type-list , interface-type

817.1.2.1 Base classes

9When a class-type is included in the class-base, it specifies the direct base class of the class being declared.

10If a non-partial class declaration has no class-base, or if the class-base lists only interface types, the direct

11base class is object. When a partial class declaration includes a base class specification, that base class

12specification shall reference the same type as all other parts of that partial type that include a base class

13specification. If no part of a partial class includes a base class specification, the base class is object. A

14class inherits members from its direct base class, as described in §17.2.1.

15[Example: In the following code

16class A {}

17class B: A {}

18class A is said to be the direct base class of B, and B is said to be derived from A. Since A does not explicitly

19specify a direct base class, its direct base class is implicitly object. end example]

20The base class specified in a class declaration can be a constructed class type (§26.5). A base class cannot be

21a type parameter on its own, though it can involve the type parameters that are in scope. [Example:

22

class

Extend<V>: V {}

//

Error, type parameter used as base class

23

class

C<V>: Base<V[]> {}

//

Ok

24end example]

25The direct base class (and any type arguments) of a class type shall be at least as accessible as the class type

26itself (§10.5.4). For example, it is a compile-time error for a public class to derive from a private or

27internal class.

28The direct base class of a class type shall not be any of the following types: System.Array,

29System.Delegate, System.Enum, or System.ValueType.

30The base classes of a class are the direct base class and its base classes (after substituting type arguments for

31type parameters in constructed generic types). In other words, the set of base classes is the transitive closure

32of the direct base class relationship. [Note: Referring to the example above, the base classes of B are A and

33object. end note]

34Except for class object, every class has exactly one direct base class. The object class has no direct base

35class and is the ultimate base class of all other classes.

36When a class B derives from a class A, it is a compile-time error for A to depend on B. A class directly

37depends on its direct base class (if any) and directly depends on the class within which it is immediately

38nested (if any). Given this definition, the complete set of classes upon which a class depends is the transitive

39closure of the directly depends on relationship.

40[Example: The example

41class A: B {}

42class B: C {}

43class C: A {}

44is in error because the classes circularly depend on themselves. Likewise, the example

45class A: B.C {}

258

Chapter 17 Classes

1class B: A

2{

3public class C {}

4}

5results in a compile-time error because A depends on B.C (its direct base class), which depends on B (its

6immediately enclosing class), which circularly depends on A. end example]

7A class does not depend on the classes that are nested within it. [Example: In the following code

8class A

9{

10class B: A {}

11}

12B depends on A (because A is both its direct base class and its immediately enclosing class), but A does not

13depend on B (since B is neither a base class nor an enclosing class of A). Thus, the example is valid. end

14example]

15It is not possible to derive from a sealed class. [Example: In the following code

16sealed class A {}

17

class B: A {}

// Error, cannot derive from a sealed class

18class B results in a compile-time error because it attempts to derive from the sealed class A. end example]

1917.1.2.2 Interface implementations

20A class-base specification can include a list of interface types, in which case the class is said to implement

21the given interface types.

22The set of interfaces for a type declared in multiple parts (§17.1.4) is the union of the interfaces specified on

23each part. A particular interface can only be named once on each part, but multiple parts can name the same

24base interface(s). There shall only be one implementation of each member of any given interface. [Example:

25In the following:

26partial class C: IA, IB {...}

27partial class C: IC {...}

28partial class C: IA, IB {...}

29the set of base interfaces for class C is IA, IB, and IC. end example]

30Typically, each part provides an implementation of the interface(s) declared on that part; however, this is not

31a requirement. A part can provide the implementation for an interface declared on a different part. [Example:

32partial class X

33{

34int IComparable.CompareTo(object o) {...}

35}

36partial class X: IComparable

37{

38...

39}

40end example]

41The base interfaces specified in a class declaration can be constructed interface types (§26.5). A base

42interface cannot be a type parameter on its own, though it can involve the type parameters that are in scope.

43[Example: The following code illustrates how a class can implement and extend constructed types:

44class C<U, V> {}

45interface I1<V> {}

46class D: C<string, int>, I1<string> {}

47class E<T>: C<int, T>, I1<T> {}

48end example]

259

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