CHAPTER 8 WORKING WITH INTERFACES
namespace CustomInterface
{
class PointyTestClass : IPointy
{
public byte Points
{
get { throw new NotImplementedException(); }
}
}
}
Note Visual Studio also supports extract interface refactoring, available from the Refactor menu. This allows
you to pull out a new interface definition from an existing class definition. For example, you might be halfway
through writing a class when it dawns on you that you can generalize the behavior into an interface (and thereby
open up the possibility of alternative implementations).
Explicit Interface Implementation
As shown earlier in this chapter, a single class or structure can implement any number of interfaces.
Given this, there is always the possibility you might implement interfaces that contain identical
members and, therefore, have a name clash to contend with. To illustrate various manners in which you
can resolve this issue, create a new Console Application named InterfaceNameClash. Now design three
interfaces that represent various locations to which an implementing type could render its output:
// Draw image to a form.
public interface IDrawToForm
{
void Draw();
}
// Draw to buffer in memory.
public interface IDrawToMemory
{
void Draw();
}
// Render to the printer.
public interface IDrawToPrinter
{
void Draw();
}
Notice that each interface defines a method named Draw(), with the identical signature (which
happen to be no arguments). If you now want to support each of these interfaces on a single class type
named Octagon, the compiler would allow the following definition:
296