Implementing an Interface
The syntax for defining an interface is very similar to the syntax for defining a class:
The optional attributes are beyond the scope of this book. In short, every .NET application contains code, data, and metadata. Attributes are objects that are embedded in your program (invisible at runtime) and contain metadata—that is, data about your classes and your program. You don’t need to worry about them for our purposes here.
Access modifiers (
private, and so forth) work just as they do with classes. (See Chapter 7 for more about access modifiers.) The
interface keyword is followed by an identifier (the interface name). It is recommended (but not required) to begin the name of your interface with a capital I (
IGetNoKickFromChampagne, and so on). We will discuss the optional base list later in this chapter.
Now, suppose you are the author of a
Document class, which specifies that
Document objects can be stored in a database. You decide to have
Document implement the
IStorable interface. It isn’t required that you do so, but by implementing the
IStorable interface, you signal to potential clients that the
Document class can be used just like any other
IStorable object. This will, for example, allow your clients to add your
Document objects to an array of
IStorable myStorableArray = new IStorable;
As we discussed earlier, ...