Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 17 INTERFACES

Declaring an Interface

The previous section used an interface that was already declared in the BCL. In this section, you’ll see how to declare interfaces.

The important things to know about declaring an interface are the following:

An interface declaration cannot contain data members.

An interface declaration can contain only declarations of the following kinds of nonstatic function members:

Methods

Properties

Events

Indexers

The declarations of these function members cannot contain any implementation code. Instead, a semicolon must be used for the body of each member declaration.

By convention, interface names begin with an uppercase I (for example, ISaveable).

Like classes and structs, interface declarations can also be split into partial interface declarations, as described in the “Partial Classes” section of Chapter 6.

414

CHAPTER 17 INTERFACES

The following code shows an example of declaring an interface with two method members:

Keyword

Interface name

 

 

Semicolon in place of body

interface IMyInterface1

{

 

 

int

DoStuff

(

int nVar1, long lVar2 );

double DoOtherStuff(

string s, long x );

}

 

 

Semicolon in place of body

There is an important difference between the accessibility of an interface and the accessibility of interface members:

An interface declaration can have any of the access modifiers public, protected, internal, or private.

Members of an interface, however, are implicitly public, and no access modifiers, including public, are allowed.

Access modifiers are allowed on interfaces.

public interface IMyInterface2

{

private int Method1( int nVar1, long lVar2 );

// Error

 

}

 

Access modifiers are NOT allowed on interface members.

415

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]