You can make your function as follows: it will take a List
and contact name
, and then remove the first occurrence of the contact name from the contact list.
public static void removeContact(List<Contact> contactList, object ContactName) { Contact contactToRemove = (Contact)contactList.AsEnumerable().Where (x => x.ContactName == ContactName || x.ContactNumber == (int)ContactName).First(); contactList.Remove(contactToRemove); }
my contact class:
class Contact { private int _ContactNumber; public int ContactNumber { get { return _ContactNumber; } set { _ContactNumber = value; } } private string _ContactName; public string ContactName { get { return _ContactName; } set { _ContactName = value; } } }
So your case would be something like this:
case 'R': Console.WriteLine("enter the contact name/Contact Number To delete"); removeContact(contactList, Console.Read().ToString() ); break;
user4959983
source share