Thuta Learning
C#
ProjectsProgrammingBeginner

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

C#Lesson 3030 min

What you'll walk away with

  • Explain the core concept and runtime behavior of Project: Contact Book – Part 2 (Search + Update + Delete)
  • Test normal, boundary, and invalid cases independently
  • Write readable, type-safe, and testable C# code

Explain it simply

Project: Contact Book – Part 2 (Search + Update + Delete), in plain language

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.

Problem → Why → Solution

The problem — why this concept matters

Using a list index as a persistent contact ID can update the wrong record after deletion or sorting.

A common mistake

Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Core concept — understand how it works

Search, update, and delete flows need stable identity, not-found behavior, duplicate policy, comparison rules, and atomic state changes. Separate each operation from the UI through a success or failure result contract.

Mental model
  1. project requirements
  2. components, state, and services
  3. tested UI

Explain the core concept and runtime behavior of Project: Contact Book – Part 2 (Search + Update + Delete)

Core syntax or pattern

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}");
    }
}

// Example Search branch in the menu
// 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");
// }

Search, update, and delete flows need stable identity, not-found behavior, duplicate policy, comparison rules, and atomic state changes. Separate each operation from the UI through a success or failure result contract.

Read the code step by step

  1. project requirementsusing System.Linq;

    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.

  2. components, state, and servicesif (c == null) return false;

    Search, update, and delete flows need stable identity, not-found behavior, duplicate policy, comparison rules, and atomic state changes. Separate each operation from the UI through a success or failure result contract.

  3. tested UI// }

    Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

Execution flow
  1. project requirements
  2. components, state, and services
  3. tested UI

Success

Explain the core concept and runtime behavior of Project: Contact Book – Part 2 (Search + Update + Delete)

Failure

Using a list index as a persistent contact ID can update the wrong record after deletion or sorting.

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}");
    }
}

// Example Search branch in the menu
// 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.

Real-world usage

How to apply it in a real project

Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

Mini challenge

Build it yourself

Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
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}");
    }
}

// Example Search branch in the menu
// 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");
// }

Expected: Explain the core concept and runtime behavior of Project: Contact Book – Part 2 (Search + Update + Delete)

Show hint

Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

Quick quiz 1 / 3

What best describes the core idea of Project: Contact Book – Part 2 (Search + Update + Delete)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • project requirements → components, state, and services → tested UI
  • Search, update, and delete flows need stable identity, not-found behavior, duplicate policy, comparison rules, and atomic state changes. Separate each operation from the UI through a success or failure result contract.
  • Using a list index as a persistent contact ID can update the wrong record after deletion or sorting.
  • Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

Unit testing C# in .NET — Microsoft Learn

Easy traps

  • Using a list index as a persistent contact ID can update the wrong record after deletion or sorting.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.

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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

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