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.
- project requirements
- components, state, and services
- tested UI
Explain the core concept and runtime behavior of Project: Contact Book – Part 2 (Search + Update + Delete)
Core syntax or pattern
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
- project requirements
using 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.
- components, state, and services
if (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.
- tested UI
// }Implement exact ID lookup and case-insensitive name search, then test zero, one, and many results plus update conflict and missing delete.
- project requirements
- components, state, and services
- 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
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'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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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