.NET
  Home arrow .NET arrow Generics and Interface-Based Programming
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 4 stars4 stars4 stars4 stars4 stars / 2
    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


    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

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek