Generics and Interface-Based Programming (Page 1 of 4 )
In this third part of a four-part series, you will learn about generic interfaces. This article is excerpted from chapter three of
Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Interfaces and Generics
Like classes or structures, interfaces too can be defined in terms of generic type parameters.* Generic interfaces provide all the benefits of interface-based programming without compromising type safety, performance, or productivity. All of what you have seen so far with normal interfaces you can also do with generic interfaces. The main difference is that when deriving from a generic interface, you must provide a specific type parameter to use instead of the generic type parameter. For example, given this definition of the generic IList<T> interface:
public interface IList<T>
{
void AddHead(T item);
void RemoveHead(T item);
void RemoveAll();
}
you can implement the interface implicitly and substitute an integer for the generic type parameter:
public class NumberList : IList<int>
{
public void AddHead(int item)
{...}
public void RemoveHead(int item)
{...}
public void RemoveAll()
{...}
//Rest of the implementation
}
When the client usesIList<T>, it must choose an implementation of the interface with a specific type parameter:
IList<int> list = new NumberList();
list.AddHead(3);
Generic interfaces allow you to define an abstract service definition (the generic interface) once, yet use it on multiple components with multiple type parameters. For example, an integer-based list can implement the interface:
public class NumberList : IList<int>
{...}
And so can a string-based list:
public class NameList : IList<string>
{...}
Once a generic interface is bounded (i.e., once you’ve specified types for it) it is considered a distinct type. Consequently, two generic interface definitions with different generic type parameters are no longer polymorphic with each other. This means that a variable of the typeIList<int>cannot be assigned to a variable or passed to a method that expects anIList<string>:
void ProcessList(IList<string> names)
{...}
IList<int> numbers = new NumberList();
ProcessList(numbers);//Does not compile
You can maintain the polymorphism with generic interfaces if you keep the use of the interface in generic type parameter terms:
public class ListClient<T>
{
public void ProcessList(IList<T> list)
{...}
}
IList<int> numbers = new NumberList();
IList<string> names = new NameList();
ListClient<int> numbersClient = new ListClient<int>();
ListClient<string> namesClient = new ListClient<string>();
//Reuse of the code and algorithms of ProcessList():
numbersClient.ProcessList(numbers);
namesClient.ProcessList(names);
Next: Deriving from a Generic Interface >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.
|
|