public int CompareTo(object obj) { PhoneContact comp = obj as PhoneContact; if (comp != null) { return(this.phone.CompareTo(comp.phone)); } else { throw new ArgumentException("Not a phone contact."); } }
static void Main(string[] args) { Contact[] arr = new Contact[5]; arr[0] = new MailContact("andrew", "andrgmailcom"); arr[1] = new MailContact("bodya", "bodgmailcom"); arr[2] = new MailContact("marian", "mariangmailcom"); arr[3] = new PhoneContact("vasyl", 234002340); arr[4] = new PhoneContact("oleg", 324823487); showInformationByName("marian", arr); showInformationByName("petro", arr); var sortedArray = arr.OrderBy(x => x.name).ToList(); Array.Sort(arr, ContactComparator); printArray(sortedArray); Console.Read(); }
public IContact Parse(string str) { string[] array = str.Split(' ', 2); IContact contact; if (Convert.ToInt32(array[0]) == 1) { contact = new MailContact(); } else if (Convert.ToInt32(array[0]) == 2) { contact = new PhoneContact(); } else { throw new Exception("invalid data"); } contact.Input(array[1]); return(contact); }