Interfaces


Earlier in this appendix, you learned that there are two types of inheritance: implementation inheritance and interface inheritance. So far you've seen implementation inheritance; this section looks more closely at interface inheritance.

In general, an interface is a contract that says that a class must implement certain features (usually methods and properties), but which doesn't specify any implementations of those methods and properties. Therefore you don't instantiate an interface; instead a class can declare that it implements one or more interfaces. In C#, as in most languages that support interfaces, this essentially means that the class inherits from the interface.

To get an idea of how an interface looks in programming terms, the following shows the syntax for the definition of an interface that is defined in the .NET base classes, IEnumerator, from theSystem.Collections namespace. IEnumerator looks like this:

 interface IEnumerator  { // Properties object Current {get; } // Methods bool MoveNext(); void Reset(); } 

As you can see, the IEnumerator interface has two methods and one property. This interface is important in implementing collections and is designed to encapsulate the functionality of moving through the items in a collection. MoveNext() moves to the next item, Reset() returns to the first item, and Current retrieves a reference to the current item.

Beyond the lack of method implementations, the main point to note is the lack of any modifiers on the members. Interface members are always public and cannot be declared as virtual or static.

So why have interfaces? Up to now you've treated classes as having certain members and not concerned yourself about grouping any members together — our classes have simply contained a list of various miscellaneous methods, fields, properties, and so on. There are often situations in which you need to know that the class implements certain features in order to be able to use a class in a certain way. An example is provided by the foreach loop in C#. In principle, it is possible to use foreach to iterate through a class instance, provided that that class is able to act as if it is a collection. How can the .NET runtime tell whether a class instance represents a collection? It queries the instance to find out whether it implements the System.Collections.IEnumerable interface. If it does, the runtime uses the methods on this interface to iterate through the members of the collection. If it doesn't, foreach will raise an exception.

You might wonder why in this case you don't just see if the class implements the required methods and properties. The answer is that that wouldn't be a very reliable way of checking. For example, you can probably think of all sorts of different reasons why a class might happen to implement a method called MoveNext() or Reset(), which don't have anything to do with collections. If the class declares that it implements the interfaces needed for collections, then you know that it really is a collection.

A second reason for using interfaces is for interoperability with COM. Before the advent of .NET, COM and its later versions DCOM and COM+, provided the main way that applications could communicate with each other on the Windows platform, and the particular object model that COM used was heavily dependent on interfaces. Indeed, it was through COM that the concept of an interface first became commonly known. It should be stressed, however, that C# interfaces are not the same as COM interfaces. COM interfaces have very strict requirements, such as that they must use GUIDs as identifiers, which are not necessarily present in C# interfaces. However, using attributes (a C# feature covered in the beginning of the book), it is possible to dress up a C# interface so it acts like a COM interface, and hence provide compatibility with COM. COM interoperability is discussed in Chapter 33, "COM Interoperability."

For more details on interfaces, see Chapter 4, "Inheritance."




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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