Information Technology Reference
In-Depth Information
What Is an Interface?
An interface is a reference type that represents a set of function members, but does not imple-
ment them. Other types—classes or structs—can implement interfaces.
To get a feeling for interfaces, I'll start by showing one that is already defined. The BCL
declares an interface called IComparable , which is shown in the code that follows. Notice that
the interface body contains the declaration of a single method, CompareTo , which takes a single
parameter of type object . Although the method has a name, parameters, and a return type,
there is no implementation. Instead, the implementation is replaced by a semicolon.
Keyword Interface name
public interface IComparable
{
int CompareTo( object obj );
}
Semicolon in place of method implementation
Figure 17-1 illustrates interface IComparable . The CompareTo method is shown in gray to
illustrate that it doesn't contain an implementation.
Figure 17-1. Representation of interface IComparable
Although the interface declaration does not provide an implementation for the method,
the .NET documentation of interface IComparable describes what the method should do, in
case you decide to create a class or struct that implements the interface. It says that when the
method is called, it should return one of the following values:
￿
A negative value, if the current object is less than the parameter object.
￿
A positive value, if the current object is greater than the parameter object.
￿
Zero, if the two objects are considered equal in the comparison.
Search WWH ::




Custom Search