Using Interfaces
In .NET programming, an interface is like a contract. It defines the public properties, methods,
and events that a class must provide to satisfy the contract. It doesn’t indicate how the class must
provide these features, however. Thats left up to the class’s code. It only defines an interface that
the class must show to the rest of the world.
In this lesson, you learn how to implement interfaces that are predefined by .NET namespaces.
You also learn how to define your own interfaces to make your code safer and more efficient.
INTERFACE ADVANTAGES
The following sections discuss two of the most important advantages provided by interfaces:
multiple inheritance and code generalization.
Multiple Inheritance
Suppose you define a Vehicle class with properties such as NumberOfPassengers,
MilesPerGallon, and NumberOfCupHolders. From this class you can derive other classes
such as
Car, PickupTruck, and Bicycle.
Suppose you also define a
Domicile class that has properties such as SquareFeet,
NumberOfBedrooms, and NumberOfBathrooms. From this class you can derive Apartment,
Condo, and VacationHome.
Next you might like to derive the
MotorHome class from both Vehicle and Domicile so it
has the properties and methods of both parent classes. Unfortunately you can’t do that in
C#. In C# a class can inherit from only a single parent class.
Though a class can have only one parent, it can implement any number of interfaces. For
example, if you turn the
Domicile class into the IDomicile interface, the MotorHome class
can inherit from
Vehicle and implement IDomicile. The interface doesn’t provide the code
needed to implement such features as the
HasAnnoyingNeighbor property, but at least it
defines that property so code that uses a
MotorHome object knows the property is available.
27
596906c27.indd 319 4/7/10 12:34:07 PM
320
LESSON 27 Using interfaces
To make recognizing interface names easy, you should begin interface names
with
I as in IDomicile, IComparable, and IWhatever.
Defining the property but not implementing it may seem like no big deal, but it lets your code treat
all
IDomicile objects in a uniform way. Instead of writing separate functions to work with Duplex,
RusticCabin, and HouseBoat objects, you can write a single function that manipulates objects that
implement
IDomicile.
That brings us to the second big advantage provided by interfaces: code generalization.
Code Generalization
Interfaces can make your code more general while still providing type checking. They let you treat
objects that have common features as if they were of the interface type rather than their true indi-
vidual types.
For example, suppose you write the following method that displays an array of strings in a
ListBox:
private void DisplayValues(string[] items)
{
itemsListBox.Items.Clear();
foreach (string item in items)
{
itemsListBox.Items.Add(item);
}
}
This function works reasonably well, but suppose you later decide that you need to display the items
that are in a
List<string> instead of an array. You could write a new version of the function that was
nearly identical to this one but that works with a list instead of an array as in the following code:
private void DisplayValues(List<string> items)
{
itemsListBox.Items.Clear();
foreach (string item in items)
{
itemsListBox.Items.Add(item);
}
}
If you compare these two functions you’ll see that they are practically identical, so if you use them
you must write, debug, and maintain two pieces of code that do almost exactly the same thing.
This is where interfaces can help.
Look again at the two functions. They differ only in their parameter definitions and the rest of their
code is the same. The reason is that the functions dont really care that the parameters are arrays or
lists. All they really care about is that you can use a
foreach loop to iterate through them.
The
IList<> interface requires that a class provide various list-like features such as Clear, Add, and
RemoveAt methods. It also requires support for foreach.
596906c27.indd 320 4/7/10 12:34:07 PM
Implementing Interfaces
321
This is a generic interface so you must provide a type parameter for it to indicate the type of items
over which the interface can loop.
Both
string[] and List<string> implement IList<string> so you can combine and generalize
the functions by making their list parameter have the type
IList<string> instead of string[] or
List<string>.
The following code shows the new version of the method. This version can display the items in a
string[], List<string>, or any other object that implements IList<string>.
private void DisplayValues(IList<string> items)
{
itemsListBox.Items.Clear();
foreach (string item in items)
{
itemsListBox.Items.Add(item.ToString());
}
}
IMPLEMENTING INTERFACES
To make a class that implements an interface, add the interface name in the class’s declaration as if
the class were inheriting from the interface. For example, the following code shows the declaration
for a
Person class that implements IComparable:
class Person : IComparable
{
...
}
You can include a class and multiple interfaces in the inheritance list. For example, the Manager
class could inherit from
Person and implement the interfaces IComparable and IDisposable.
The only other thing you need to do is implement the properties, methods, and events defined by the
interface. The
IComparable interface defines a CompareTo function that takes an object as a parameter
and returns an integer that is less than, equal to, or greater than zero to indicate whether the object
should be considered less than, equal to, or greater than the parameter.
For example, suppose the
Person class defines FirstName and LastName properties. The following
code implements a version of
CompareTo that orders Person objects according to their last names first:
// Compare this Person to another Person.
public int CompareTo(object obj)
{
// Make sure the object is a Person.
if (!(obj is Person))
throw new ArgumentOutOfRangeException(“obj”,
“Argument to CompareTo is not a Person”);
// Convert the object into a Person.
Person other = (Person)obj;
596906c27.indd 321 4/7/10 12:34:08 PM
322
LESSON 27 Using interfaces
// If our last name comes first, we come first.
if (LastName.CompareTo(other.LastName) < 0) return -1;
// If our last name comes second, we come second.
if (LastName.CompareTo(other.LastName) > 0) return 1;
// If our last names are the same, compare first names.
return FirstName.CompareTo(other.FirstName);
}
The method first checks whether its parameter is actually a Person and throws an exception if it is
really something else. Next it casts its parameter from a non-specific
object into a Person, and com-
pares the current object’s
LastName to that of the parameter Person. If the current object’s LastName
comes alphabetically before the other object’s
LastName, the function returns –1. If the current objects
LastName comes alphabetically after the other object’s LastName, the function returns 1.
If the two objects’
LastNames are the same, the function compares their FirstNames similarly and
returns the result.
The easiest way to implement an interface is to let Visual Studio build a default implementation
for you. Click on the interface in the class declaration and look for the change suggestion bar to
appear. You can see it under the “I” in
IComparable in Figure 27-1.
FIGURE 271
Move the mouse over this bar and wait until a small icon arrow with a dropdown arrow appears.
Click the arrow and select the Implement Interface command, as shown in Figure 27-2.
FIGURE 272
596906c27.indd 322 4/7/10 12:34:08 PM
Implementing Interfaces
323
When you select that command, Visual Studio adds placeholder code to satisfy the interface. The
following code shows the placeholder method for the
ICompare interface.
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
Now you can fill in the code you want to use.
There are several ways you can learn more about what an interface is for and what it does. You
can always search the online help. You can also right-click on the interface’s name and select Go
To Definition to see information as shown in Figure 27-3. Click the plus signs on the left to view
detailed comments describing the purposes of the pieces of code.
FIGURE 273
Finally you can open the Object Browser (use the View menu’s Object Browser command) and
search for the interface’s name. Select the interface in the browser’s left panel. Click an item in the
right panel for more details, as shown in Figure 27-4.
FIGURE 274
596906c27.indd 323 4/7/10 12:34:08 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.197.95