CHAPTER 8 WORKING WITH INTERFACES
Source Code The CustomInterface project is located under the Chapter 8 subdirectory.
Implementing Interfaces Using Visual Studio
Although interface-based programming is a very powerful technique, implementing interfaces may
entail a healthy amount of typing. Given that interfaces are a named set of abstract members, you are
required to type in the definition and implementation for each interface method on each type that
supports the behavior. Therefore, if you want to support an interface that defines a total of five methods
and three properties, you need to account for all eight members (or else you will receive compiler
errors).
As you would hope, Visual Studio supports various tools that make the task of implementing
interfaces less burdensome. By way of a simple test, insert a final class into your current project named
PointyTestClass. When you implement IPointy (or any interface for that matter), you might have
noticed that when you complete typing the interface’s name (or when you position the mouse cursor on
the interface name in the code window), the first letter is underlined (formally termed a smart tag).
When you click the smart tag, you will be presented with a drop-down list that allows you to implement
the interface (see Figure 8-5).
Figure 8-5. Implementing interfaces using Visual Studio
Notice you are presented with two options, the second of which (explicit interface implementation)
will be examined in the next section. For the time being, select the first option and you’ll see that Visual
Studio has generated stub code for you to update (note that the default implementation throws a
System.NotImplementedException, which can obviously be deleted).
295