CHAPTER 8 WORKING WITH INTERFACES
Now, insert a new interface into your project named IPointy using the Project Add New Item
menu option, as shown in Figure 8-1.
Figure 8-1. Interfaces, like classes, can be defined in any *.cs file
At a syntactic level, an interface is defined using the C# interface keyword. Unlike a class, interfaces
never specify a base class (not even System.Object; however, as you will see later in this chapter, an
interface can specify base interfaces). Moreover, the members of an interface never specify an access
modifier (as all interface members are implicitly public and abstract). To get the ball rolling, here is a
custom interface defined in C#:
// This interface defines the behavior of "having points."
public interface IPointy
{
// Implicitly public and abstract.
byte GetNumberOfPoints();
}
Remember that when you define interface members, you do not define an implementation scope
for the members in question. Interfaces are pure protocol and, therefore, never define an
implementation (that is up to the supporting class or structure). Hence, the following version of IPointy
would result in various compiler errors:
// Ack! Errors abound!
public interface IPointy
{
// Error! Interfaces cannot have data fields!
public int numbOfPoints;
285