Interfaces

An interface is a .NET reference type that specifies the public members of a type. They are inherited, and any type that inherits an interface must implement the members specified in the interface. Another way to view an interface is that it specifies a contract, in which any type implementing that interface promises to implement its members.

The benefits of interfaces include ease of development and maintenance and more robust code. Because an interface is something that may be used commonly across many classes, interfaces are easily identifiable. For example, a type that implements IDisposable has a Dispose method, which supports the Dispose pattern for unmanaged resource collection. Another example is the IEnumerable interface, which is used by the foreach statement to iterate through arrays and collections. Additionally, code with interfaces is more robust because the C# compiler ensures that every member of an inherited interface is implemented. Listing 4.8 shows the syntax of implementing an interface.

Listing 4.8 Interface Syntax (InterfaceSyntax.cs)
 using System; // interface to inherit and implement public interface MyInterface {    event EventHandler MyEvent;    void MyMethod();    int this[int index]    {       get;       set;    }    int MyProperty    {       get;       set;    } } // type inheriting interface public class MyClass : MyInterface {    public event EventHandler MyEvent;    public void MyMethod()    {       // some method implementation    }    public int this[int index]    {       get       {          return 0;       }       set       {          // some set accessor implementation       }    }    public int MyProperty    {       get       {          return 0;       }       set       {          // some set accessor implementation       }    } } class InterfaceSyntax {    static void Main()    {    } } 

The interface MyInterface in Listing 4.8 shows how to declare each type member. Notice that fields are not present because interfaces can't specify them. MyClass implements MyInterface by specifying an inheritance relationship in the declaration. Classes and structs can implement multiple interfaces by separating each with a comma in their declaration. Because MyClass inherits MyInterface, the C# compiler forces it to implement each MyInterface member.

SHOP TALK
UNDERSTANDING INTERFACES

Interfaces are one of the hardest concepts in C# for new developers to learn. A frequently asked question is "If I make sure that the methods I need are added to a class, why do I need an interface?" The answer is that they provide a fairly type-safe means of building routines that accept objects when you don't know the specific type of object that will be passed ahead of time. The only thing you know about the objects that will be passed to your routine are that they have specific members that must be present for your routine to be able to work with that object.

The best example I can give of the need for interfaces is in a team environment. Interfaces help define how different components talk to each other. By using an interface, you eliminate the possibility that a developer will misinterpret what members they must add to a type or how they will call another type that defines an interface. Without an interface, errors creep into the system and don't show up until runtime, when they are hard to find. With interfaces, errors in defining a type are caught immediately at compile time, where the cost is much less.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net