Книга: C# 2008 Programmer

Generic Interfaces

Generic Interfaces

Generics can also be applied on interfaces. The following example defines the IMyStack interface:

interface IMyStack<T> where T : IComparable<T> {
 void Push(T item);
 T Pop();
 bool Find(T keyword);

A class implementing a generic interface must supply the same type parameter as well as satisfy the constraints imposed by the interface.

The following shows the generic MyStack class implementing the generic IMyStack interface:

public class MyStack<T> : IMyStack<T>
 where T : IComparable<T> {

Figure 9-4 shows the error reported by Visual Studio 2008 if the generic MyStack class does not provide the constraint imposed by the generic interface.

Figure 9-4

Оглавление книги

Генерация: 0.068. Запросов К БД/Cache: 0 / 0
Вверх Вниз