Information Technology Reference
In-Depth Information
// other details elided
private string
vendorName;
}
The Employee, Customer, and Vendor classes should not share a common
base class. But they do share some properties: names (as shown earlier),
addresses, and contact phone numbers. You could factor out those prop-
erties into an interface:
public interface
IContactInfo
{
string
Name {
get
; }
PhoneNumber
PrimaryContact {
get
; }
PhoneNumber
Fax {
get
; }
Address
PrimaryAddress {
get
; }
}
public class
Employee
:
IContactInfo
{
// implementation elided.
}
This new interface can simplify your programming tasks by letting you
build common routines for unrelated types:
public void
PrintMailingLabel(
IContactInfo
ic)
{
// implementation deleted.
}
This one routine works for all entities that implement the IContactInfo
interface. Customer, Employee, and Vendor all use the same routine—but
only because you factored them into interfaces.
Using interfaces also means that you can occasionally save an unboxing
penalty for
structs
. When you place a
struct
in a box, the box supports
all interfaces that the
struct
supports. When you access the
struct
through the interface pointer, you don't have to unbox the
struct
to
access that object. To illustrate, imagine this
struct
that defines a link and
a description:
public struct
URLInfo
:
IComparable
<
URLInfo
>,
IComparable
{