.NET
  Home arrow .NET arrow Generics and Interface-Based Programming
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Generics and Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-06-07

    Table of Contents:
  • Generics and Interface-Based Programming
  • Deriving from a Generic Interface
  • Generic Interfaces as Operators
  • Generic Derivation Constraints

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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);

    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming .NET Components, Second...
     

    Buy this book now. 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.

    .NET ARTICLES

    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET
    - Understanding Interface-Based Programming
    - Generics and Interface-Based Programming
    - Delving Deeper into Interface-Based Programm...
    - Interface-Based Programming
    - The Why and How of the SplitContainer Control
    - Building an ASP.NET 2.0 Master Page in Three...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway