for any type of telephone list. It also guarantees that only valid types are used to
object. Notice that
or number is not found. This is a custom exception that is declared as shown here:
/* Implement all of the Exception constructors. Notice that
the constructors simply execute the base class constructor.
PART I
C h a p t e r 1 8 :
G e n e r i c s
509
PART IPART I
public NotFoundException() : base() { }
public NotFoundException(string str) : base(str) { }
public NotFoundException(string str, Exception inner) :
base(str, inner) { }
protected NotFoundException(
System.Runtime.Serialization.SerializationInfo si,
System.Runtime.Serialization.StreamingContext sc) :
base(si, sc) { }
}
Although only the default constructor is used by this example,
NotFoundException
implements all of the constructors defined by
Exception
for the sake of illustration.
Notice that these constructors simply invoke the equivalent base class constructor
defined by
Exception
. Because
NotFoundException
adds nothing to
Exception
, there
is no reason for any further action.
The following program puts together all the pieces and demonstrates
PhoneList
. Notice
that a class called
EmailFriend
is also created. This class does not inherit
PhoneNumber
.
Thus, it
cannot
be used to create a
PhoneList
.
// A more practical demonstration of a base class constraint.
using System;
// A custom exception that is thrown if a name or number is not found.
class NotFoundException : Exception {
/* Implement all of the Exception constructors. Notice that
the constructors simply execute the base class constructor.
Because NotFoundException adds nothing to Exception,
there is no need for any further actions. */
public NotFoundException() : base() { }
public NotFoundException(string str) : base(str) { }
public NotFoundException(string str, Exception inner) :
base(str, inner) { }
protected NotFoundException(
System.Runtime.Serialization.SerializationInfo si,
System.Runtime.Serialization.StreamingContext sc) :
base(si, sc) { }
}
// A base class that stores a name and phone number.
class PhoneNumber {
public PhoneNumber(string n, string num) {
Name = n;
Number = num;
}
public string Number { get; set; }
public string Name { get; set; }
}
// A class of phone numbers for friends.
class Friend : PhoneNumber {
www.freepdf-books.com
510
P a r t I :
T h e C # L a n g u a g e
public Friend(string n, string num, bool wk) :
base(n, num)
{
IsWorkNumber = wk;
}
public bool IsWorkNumber { get; private set; }
// ...
}
// A class of phone numbers for suppliers.
class Supplier : PhoneNumber {
public Supplier(string n, string num) :
base(n, num) { }
// ...
}
// Notice that this class does not inherit PhoneNumber.
class EmailFriend {
// ...
}
// PhoneList can manage any type of phone list
// as long as it is derived from PhoneNumber.
class PhoneList where T : PhoneNumber {
T[] phList;
int end;
public PhoneList() {
phList = new T[10];
end = 0;
}
// Add an entry to the list.
public bool Add(T newEntry) {
if(end == 10) return false;
phList[end] = newEntry;
end++;
return true;
}
// Given a name, find and return the phone info.
public T FindByName(string name) {
for(int i=0; i
// Name can be used because it is a member of
// PhoneNumber, which is the base class constraint.
if(phList[i].Name == name)
return phList[i];
}
www.freepdf-books.com