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

Chapter 20 Interfaces

120. Interfaces

2An interface defines a contract. A class or struct that implements an interface shall adhere to its contract. An

3interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces.

4Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide

5implementations for the members that it declares. The interface merely specifies the members that shall be

6supplied by classes or interfaces that implement the interface.

720.1 Interface declarations

8An interface-declaration is a type-declaration (§16.6) that declares a new interface type.

9interface-declaration:

10

attributesopt interface-modifiersopt partialopt interface identifier type-parameter-listopt

11

interface-baseopt type-parameter-constraints-clausesopt interface-body ;opt

12An interface-declaration consists of an optional set of attributes (§24), followed by an optional set of

13interface-modifiers (§20.1.1), followed by an optional partial modifier (§17.1.4), followed by the

14keyword interface and an identifier that names the interface, followed by an optional type-parameter-list

15(§26.1.1), followed by an optional interface-base specification (§20.1.2), followed by an optional type-

16parameter-constraints-clauses (§26.7), followed by an interface-body (§20.1.3), optionally followed by a

17semicolon.

18An interface declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-

19parameter-list.

20An interface declaration that supplies a type-parameter-list is a generic interface declaration (§26.3).

2120.1.1 Interface modifiers

22An interface-declaration can optionally include a sequence of interface modifiers:

23interface-modifiers:

24

interface-modifier

25

interface-modifiers interface-modifier

26

interface-modifier:

27

new

28

public

29

protected

30

internal

31private

32It is a compile-time error for the same modifier to appear multiple times in an interface declaration.

33The new modifier is only permitted on nested interfaces. It specifies that the interface hides an inherited

34member by the same name, as described in §17.2.2.

35The public, protected, internal, and private modifiers control the accessibility of the interface.

36Depending on the context in which the interface declaration occurs, only some of these modifiers might be

37permitted (§10.5.1). When a partial type declaration (§17.1.4) includes an accessibility specification (via the

38public, protected, internal, and private modifiers), the rules in §17.1.1 apply.

335

C# LANGUAGE SPECIFICATION

120.1.2 Base interfaces

2An interface can inherit from one or more interfaces, which are called the explicit base interfaces of the

3interface. When an interface has one or more explicit base interfaces, then in the declaration of that interface,

4the interface identifier is followed by a colon and a comma-separated list of base interfaces.

5interface-base:

6: interface-type-list

7The explicit base interfaces can be constructed interface types (§26.5). A base interface cannot be a type

8parameter on its own, though it can involve the type parameters that are in scope.

9The explicit base interfaces (and any type arguments) of an interface shall be at least as accessible as the

10interface itself (§10.5.4). [Note: For example, it is a compile-time error to specify a private or internal

11interface in the interface-base of a public interface. end note]

12It is a compile-time error for an interface to directly or indirectly inherit from itself.

13The base interfaces of an interface are the explicit base interfaces and their base interfaces (after substituting

14type arguments for type parameters in constructed generic types). In other words, the set of base interfaces is

15the complete transitive closure of the explicit base interfaces, their explicit base interfaces, and so on. An

16interface inherits all members of its base interfaces. [Example: In the following code

17interface IControl

18{

19void Paint();

20}

21interface ITextBox: IControl

22{

23void SetText(string text);

24}

25interface IListBox: IControl

26{

27void SetItems(string[] items);

28}

29interface IComboBox: ITextBox, IListBox {}

30the base interfaces of IComboBox are IControl, ITextBox, and IListBox. In other words, the

31IComboBox interface above inherits members SetText and SetItems as well as Paint. end example]

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. [Example: In the following code

35interface IBase<T>

36{

37T[] Combine(T a, T b);

38}

39interface IDerived : IBase<string[,]>

40{

41// Inherited: string[][,] Combine(string[,] a, string[,] b);

42}

43the interface IDerived inherits the Combine method after the type parameter T is replaced with

44string[,]. end example]

45A class or struct that implements an interface also implicitly implements all of the interface’s base

46interfaces.

47The handling of interfaces on multiple parts of a partial interface declaration (§17.1.4) are discussed further

48in §17.1.2.2.

4920.1.3 Interface body

50The interface-body of an interface defines the members of the interface.

336

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