.NET
  Home arrow .NET arrow Understanding 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

Understanding Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-06-14

    Table of Contents:
  • Understanding Interface-Based Programming
  • Designing and Factoring Interfaces
  • Factoring Metrics
  • Interfaces in Visual Studio 2005
  • Interface Refactoring

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry 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!

    Understanding Interface-Based Programming
    (Page 1 of 5 )

    In this final part of a four-part series focused on interface-based programming, you'll learn about interface factoring, factoring metrics, and more. 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.

    Generic Interface Methods

    In C# 2.0, an interface method can define generic type parameters, specific to its execution scope:

      public interface IMyInterface<T>
      {
        void MyMethod<X>(T t,X x);
      }

    This is an important capability, because it allows you to call the method with a different type every time, which is often very handy for utility classes.

    You can define method-specific generic type parameters even if the containing interface does not use generics at all:

      public interface IMyInterface
      {
        void MyMethod<T>(T t);
      }

    This ability is for methods only. Properties or indexers can use only generic type parameters defined at the scope of the interface.

    When you call an interface method that defines generic type parameters, you can provide the type to use at the call site:

      public class MyClass : IMyInterface
      {
        public void MyMethod<T>(T t)
        {...}
     
    }
      IMyInterface obj = new MyClass();
      obj.MyMethod<int>(3);

    That said, when the method is invoked, the C# compiler is smart enough to infer the correct type based on the type of parameter passed in, and it allows you to omit the type specification altogether:

      IMyInterface obj = new MyClass();
      obj.MyMethod(3);

    This ability is called generic type inference. Note that the compiler cannot infer the type based on the type of the returned value alone:

      public interface IMyInterface
      {
       
    T MyMethod<T>();
      }
      public class MyClass : IMyInterface
      {
       
    public T MyMethod<T>()
       
    {...}
      }
      IMyInterface obj = new MyClass();
      int number = obj.MyMethod();//Does not compile

    When an interface method defines its own generic type parameters, it can also define constraints for these types:

      public interface IMyInterface
      {
       
    void MyMethod<T>(T t) where T : IComparable<T>;
      }

    However, my recommendation to avoid constraints at the interface level extends to method-level generic type parameters as well.

    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...




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