.NET
  Home arrow .NET arrow Delving Deeper into Interface-Based Progra...
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 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Delving Deeper into Interface-Based Programming
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-05-31

    Table of Contents:
  • Delving Deeper into Interface-Based Programming
  • Interfaces and Partial Types
  • Implementing Multiple Interfaces
  • Interfaces and Class Hierarchies

  • 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!

    Delving Deeper into Interface-Based Programming


    (Page 1 of 4 )

    In this second article in a four part series, you'll learn about interface methods, properties, and events; how to implement multiple interfaces; and more. It 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.

    Interface Methods, Properties, and Events

    An interface isn’t limited only to defining methods. An interface can also define properties, indexers, and events. Example 3-7 shows the syntax for defining all of these in an interface and the corresponding implementation.

    Example 3-7.  An interface can define methods, properties, indexers, and events

    public delegate void NumberChangedEventHandler(int number);

    public interface IMyInterface
    {
     
    void Method1(); //A method
     
    int SomeProperty{ get; set; }//A property
     
    int this[int index]{ get; set;}//An indexer
     
    event NumberChangedEventHandler NumberChanged;//An event
    }

    public class MyClass : IMyInterface
    {
     
    public event NumberChangedEventHandler NumberChanged;

      public void Method1()
     
    {...}
     
    public int SomeProperty
     
    {
       
    get
       
    {...}
       
    set
       
    {...}
     
    }
     
    public int this[int index]
     
    {
       
    get
       
    {...}
       
    set
       
    {...}
     
    }
    }

    Interfaces and Structs

    An interesting use of interfaces with properties involves structs. In .NET, a struct (a Structure in Visual Basic 2005) can’t have a base struct or a base class, because it’s a value type. However, .NET does permit structs to implement one or more interfaces. The reason for this is that sometimes you want to define abstract data storage, and there are a number of possible implementations for the actual structure. By defining an interface (preferably with properties only, but it can have methods as well), you can pass around the interface instead of the actual struct and gain the benefits of polymorphism, even though structs aren’t allowed to derive from a common base struct. Example 3-8 demonstrates the use of an interface (with properties only) as a base type for structs.

    Example 3-8.  Using an interface as a base type for structs

    public interface IMyBaseStruct
    {
      int    SomeNumber{ get; set; }
      string SomeString{ get; set; }
    }

    struct MyStruct : IMyBaseStruct
    {
      public int SomeNumber
      { get{...} set{...} }
      public string SomeString
      { get{...} set{...} }
      //Rest of the implementation
    }
    struct MyOtherStruct : IMyBaseStruct
    {
     
    public int SomeNumber
      { get{...} set{...} }
      public string SomeString
      { get{...} set{...} }
      //Rest of the implementation
    }
    //A method that accepts a struct, without knowing exactly the type
    public void DoWork(IMyBaseStruct storage)
    {...}

    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

    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - 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 ...

    Iron Speed




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