Skip to Main Content
Programming .NET Components, 2nd Edition
book

Programming .NET Components, 2nd Edition

by Juval Lowy
July 2005
Intermediate to advanced content levelIntermediate to advanced
644 pages
17h
English
O'Reilly Media, Inc.
Content preview from Programming .NET Components, 2nd Edition

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 uses IList<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., ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Windows Forms Programming in C#

Windows Forms Programming in C#

Chris Sells
Metaprogramming in .NET

Metaprogramming in .NET

Jason Bock, Kevin Hazzard
.NET Windows Forms in a Nutshell

.NET Windows Forms in a Nutshell

Ian Griffiths, Matthew Adams

Publisher Resources

ISBN: 0596102070Supplemental ContentErrata Page