Thuta Learning
ProjectsProgrammingbeginner

Project: Contact Book – Part 2 (Search + Update + Delete)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Project: Contact Book – Part 2 (Search + Update + Delete) in a real project
  • Write and run the code yourself
  • Build a whole project step by step

Let's think this through for a moment

In Part 1 we already wrote Add and Show All — now in Part 2, we'll make the Contact Book more useful by adding Search, Update, and Delete. This is where we'll put LINQ (Where, FirstOrDefault) and List methods from the intermediate/advanced chapter to real use. Searching by name and updating/deleting a contact's information are the essential CRUD operations you'll find in real-world apps. By the end of this part, the Contact Book will actually function as a real application.

Let's actually build it

In the ContactManager class, write a FindByName(string name) method using LINQ's FirstOrDefault(). Add an UpdateContact(string name, string newPhone, string newEmail) method that uses FindByName() to find the contact and update its Phone/Email. Add a DeleteContact(string name) method that uses Remove() to delete the contact from the list. Add "3. Search", "4. Update", and "5. Delete" options to the menu and wire them up with user input. Show a "Contact not found" message if no match is found.

Code example

csharp
using System.Linq;

class ContactManager
{
    private List<Contact> contacts = new List<Contact>();

    public void AddContact(Contact c) => contacts.Add(c);

    public Contact FindByName(string name)
    {
        return contacts.FirstOrDefault(c => c.Name.ToLower() == name.ToLower());
    }

    public bool UpdateContact(string name, string newPhone, string newEmail)
    {
        Contact c = FindByName(name);
        if (c == null) return false;

        c.Phone = newPhone;
        c.Email = newEmail;
        return true;
    }

    public bool DeleteContact(string name)
    {
        Contact c = FindByName(name);
        if (c == null) return false;

        contacts.Remove(c);
        return true;
    }

    public void ShowAll()
    {
        foreach (Contact c in contacts)
            Console.WriteLine($"{c.Name} | {c.Phone} | {c.Email}");
    }
}

// Menu ထဲက Search branch ဥပမာ
// else if (choice == "3")
// {
//     Console.Write("Search name: ");
//     string searchName = Console.ReadLine();
//     Contact found = manager.FindByName(searchName);
//     if (found != null)
//         Console.WriteLine($"Found: {found.Name} | {found.Phone} | {found.Email}");
//     else
//         Console.WriteLine("Contact not found");
// }
You should see
You'll end up with an app that can search contacts by name, edit the phone/email of an existing contact, and delete a contact when needed.

5-minute challenge

Write a SearchByPhone(string phone) method that uses LINQ's Where() to return all contacts whose phone number matches a partial string (like "09").

A quick word of caution

Remember that LINQ's FirstOrDefault() returns null when nothing matches — always check for null.

Easy traps

  • Comparing strings directly with Name == name without considering case sensitivity, ending up with a "Contact not found" when it shouldn't happen
  • Not checking whether FindByName() returned null before accessing properties directly in Update/Delete, causing a NullReferenceException

Now try it yourself

Write a SearchByPhone(string phone) method that uses LINQ's Where() to return all contacts whose phone number matches a partial string (like "09").

You'll know it worked when: You'll end up with an app that can search contacts by name, edit the phone/email of an existing contact, and delete a contact when needed.