class NotFoundException : Exception {
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) { }
public interface IPhoneNumber {
string Number { get; set; }
string Name { get; set; }
class Friend : IPhoneNumber {
public Friend(string n, string num, bool wk) {
public bool IsWorkNumber { get; private set; }
public string Number { get; set; }
public string Name { get; set; }
class Supplier : IPhoneNumber {
public Supplier(string n, string num) {
public string Number { get; set; }
public string Name { get; set; }
class PhoneList<T> where T : IPhoneNumber {
public bool Add(T newEntry) {
if(end == 10) return false;
public T FindByName(string name) {
for (int i=0; i<end; i++) {
if(phList[i].Name == name)
throw new NotFoundException();
public T FindByNumber(string number) {
for(int i=0; i<end; i++) {
if(phList[i].Number == number)
throw new NotFoundException();
public class UseBaseClassConstraint {
public static void Main() {
PhoneList<Friend> plist = new PhoneList<Friend>();
plist.Add(new Friend("Miha", "6666-666", true));
plist.Add(new Friend("Гари", "555-6756", true));
plist.Add(new Friend("Матт", "555-9254", false));
Friend frnd = plist.FindByName("Miha");
Console.Write("(by name) - " + frnd.Name + " " + frnd.Number);
Console.WriteLine(" (рабочий)");
catch(NotFoundException) {
Console.WriteLine("He найдено");
Friend frnd = plist.FindByNumber("6666-666");
Console.Write("(by number) - " + frnd.Name + " " + frnd.Number);
Console.WriteLine(" (рабочий)");
catch(NotFoundException) {
Console.WriteLine("He найдено");
Console.WriteLine(new String('-',45));
PhoneList<Supplier> plist2 = new PhoneList<Supplier>();
plist2.Add(new Supplier("Фирма Global Hardware", "555-8834"));
plist2.Add(new Supplier("Агентство Computer Warehouse", "555-9256"));
plist2.Add(new Supplier("Компания NetworkCity", "555-2564"));
Supplier sp = plist2.FindByNumber("555-2564");
Console.WriteLine("(by number) - " + sp.Name + " " + sp.Number);
catch(NotFoundException) {
Console.WriteLine("He найдено");