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

CHAPTER 19 GENERICS

Generic Interfaces

Generic interfaces allow you to write interfaces where the formal parameters and return types of interface members are generic type parameters. Generic interface declarations are similar to nongeneric interface declarations but have the type parameter list in angle brackets after the interface name.

For example, the following code declares a generic interface called IMyIfc.

Simple is a generic class that implements generic interface IMyIfc.

Main instantiates two objects of the generic class: one with type int and the other with type string.

Type parameter

interface IMyIfc<T> // Generic interface

{

T ReturnIt(T inValue);

}

Type parameter Generic interface

class Simple<S> : IMyIfc<S>

// Generic class

{

 

 

 

 

public

S ReturnIt(S

inValue)

// Implement generic interface

{ return inValue; }

 

 

}

 

 

 

 

class Program

 

 

 

{

 

 

 

 

static

void Main()

 

 

 

{

 

 

 

 

var

trivInt

=

new Simple<int>();

 

var

trivString =

new Simple<string>();

 

Console.WriteLine("{0}", trivInt.ReturnIt(5)); Console.WriteLine("{0}", trivString.ReturnIt("Hi there."));

}

}

This code produces the following output:

5

Hi there.

491

CHAPTER 19 GENERICS

An Example Using Generic Interfaces

The following example illustrates two additional capabilities of generic interfaces:

Like other generics, instances of a generic interface instantiated with different type parameters are different interfaces.

You can implement a generic interface in a nongeneric type.

For example, the following code is similar to the last example, but in this case, Simple is a nongeneric class that implements a generic interface. In fact, it implements two instances of IMyIfc. One instance is instantiated with type int and the other with type string.

interface IMyIfc<T> // Generic interface

{

T ReturnIt(T inValue);

}

Two different interfaces from the same generic interface

 

 

 

 

class Simple : IMyIfc<int>, IMyIfc<string>

// Non-generic class

{

 

 

 

 

 

public int ReturnIt(int inValue)

// Implement interface using int

{ return inValue; }

 

 

 

 

public string ReturnIt(string inValue)

// Implement interface using string

{ return inValue; }

 

 

 

 

}

 

 

 

 

 

class Program

{

static void Main()

{

Simple trivial = new Simple();

Console.WriteLine("{0}", trivial.ReturnIt(5)); Console.WriteLine("{0}", trivial.ReturnIt("Hi there."));

}

}

This code produces the following output:

5

Hi there.

492

CHAPTER 19 GENERICS

Generic Interface Implementations Must Be Unique

When implementing an interface in a generic type, there must be no possible combination of type arguments that would create a duplicate interface in the type.

For example, in the following code, class Simple uses two instantiations of interface IMyIfc.

The first one is a constructed type, instantiated with type int.

The second one has a type parameter rather than an argument.

There's nothing wrong in itself with the second interface, since it’s perfectly fine to use a generic interface. The problem here, though, is that it allows a possible conflict, because if int is used as the type argument to replace S in the second interface, then Simple would have two interfaces of the same type— which is not allowed.

interface IMyIfc<T>

{

T ReturnIt(T inValue);

}

 

Two interfaces

 

 

 

 

 

 

 

 

 

 

 

 

 

 

class Simple<S> : IMyIfc<int>, IMyIfc<S>

// Error!

{

 

 

 

 

 

 

 

public int ReturnIt(int inValue)

//

Implement first interface.

{

 

 

 

 

 

 

 

return inValue;

 

 

 

 

 

 

}

 

 

 

 

 

 

 

public S ReturnIt(S inValue)

//

Implement second interface,

{

 

 

 

 

//

but if it's int, it would be

return inValue;

 

 

 

//

the same as the one above.

}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

Note The names of generic interfaces do not clash with nongeneric interfaces. For example, in the preceding code, we could have also declared a nongeneric interface named IMyIfc.

493

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