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

CHAPTER 17 INTERFACES

An Interface Is a Reference Type

An interface is more than just a list of members for a class or struct to implement. It is a reference type. You cannot access an interface directly through the class object’s members. You can, however, get a

reference to the interface by casting the class object reference to the type of the interface. Once you have a reference to the interface, you can use dot-syntax notation with the reference to call interface members.

For example, the following code shows an example of getting an interface reference from a class object reference.

In the first statement, variable mc is a reference to a class object that implements interface IIfc1. The statement casts that reference to a reference to the interface and assigns it to variable ifc.

The second statement uses the reference to the interface to call the implementation method.

Interface Cast to interface

 

 

IIfc1 ifc = (IIfc1) mc;

// Get ref to interface

 

Interface ref

Class object ref

 

ifc.PrintOut ("interface");

// Use ref to interface to call member

 

 

 

 

Use dot-syntax notation to call through the interface reference.

For example, the following code declares an interface and a class that implements it. The code in Main creates an object of the class and calls the implementation method through the class object. It also creates a variable of the interface type, casts the reference of the class object to the interface type, and calls the implementation method through the reference to the interface. Figure 17-3 illustrates the class and the reference to the interface.

418

CHAPTER 17 INTERFACES

interface IIfc1

{

void PrintOut(string s);

}

class MyClass: IIfc1

{

public void PrintOut(string s)

{

Console.WriteLine("Calling through: {0}", s);

}

}

 

class Program

 

{

 

static void Main()

 

{

 

MyClass mc = new MyClass();

// Create class object

mc.PrintOut("object");

// Call class object implementation method

IIfc1 ifc = (IIfc1)mc;

// Cast class object ref to interface ref

ifc.PrintOut("interface");

// Call interface method

}

 

}

 

This code produces the following output:

Calling through: object

Calling through: interface

Figure 17-3. A reference to the class object and a reference to the interface

419

CHAPTER 17 INTERFACES

Using the as Operator with Interfaces

In the previous section, you saw that you can use the cast operator to get a reference to an object’s interface. An even better idea is to use the as operator. The as operator is covered in detail in Chapter 18, but I’ll mention it here as well, since it’s a good choice to use with interfaces.

If you attempt to cast a class object reference to a reference of an interface that the class doesn’t implement, the cast operation will raise an exception. You can avoid this problem by using the as operator instead. It works as follows:

If the class implements the interface, the expression returns a reference to the interface.

If the class doesn’t implement the interface, the expression returns null rather than raising an exception.

The following code demonstrates the use of the as operator. The first line uses the as operator to obtain an interface reference from a class object. The result of the expression sets the value of b either to null or to a reference to an ILiveBirth interface.

The second line checks the value of b and, if it is not null, executes the command that calls the interface member method.

Class object ref

Interface name

 

 

 

ILiveBirth b = a as ILiveBirth;

// Acts like cast: (ILiveBirth)a

 

 

Interface

Operator

 

ref

 

 

 

if (b != null)

Console.WriteLine("Baby is called: {0}", b.BabyCalled());

420

CHAPTER 17 INTERFACES

Implementing Multiple Interfaces

In the examples shown so far, the classes have implemented a single interface.

A class or struct can implement any number of interfaces.

All the interfaces implemented must be listed in the base class list and separated by commas (following the base class name, if there is one).

For example, the following code shows class MyData, which implements two interfaces: IDataStore and IDataRetrieve. Figure 17-4 illustrates the implementation of the multiple interfaces in class MyData.

interface

IDataRetrieve { int GetData(); }

// Declare interface

interface

IDataStore { void SetData( int x ); }

// Declare interface

 

Interface

Interface

 

 

 

class MyData: IDataRetrieve, IDataStore

// Declare class

{

 

 

 

int Mem1;

 

// Declare field

public

int GetData()

{ return Mem1;

}

public

void SetData( int x ) { Mem1 = x;

}

}

 

 

 

class Program

 

 

{

 

 

 

static

void Main()

 

// Main

{

MyData data = new MyData(); data.SetData( 5 );

Console.WriteLine("Value = {0}", data.GetData());

}

}

This code produces the following output:

Value = 5

Figure 17-4. Class implementing multiple interfaces

421

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