Thuta Learning
C#
ProjectsProgrammingBeginner

Project: Contact Book – Part 1 (Setup + Core Structure)

C#Lesson 2930 min

What you'll walk away with

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

Explain it simply

Project: Contact Book – Part 1 (Setup + Core Structure), in plain language

In this project we'll build a simple Contact Book console app that stores and lets you edit contacts (name, phone, email). We'll reuse the class, object, property, and constructor concepts from the basics chapter to learn how to model a real-world data structure. In Part 1, we'll start by writing the foundation: a Contact class, and a ContactManager class that stores contacts in a List. Get this structure solid, and adding more features in later parts will be a breeze. We'll also handle user input with Console.ReadLine() and build out the basic Add Contact feature.

Problem → Why → Solution

The problem — why this concept matters

Allowing invalid names or phone values to enter objects anywhere scatters validation across the application.

A common mistake

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

Core concept — understand how it works

For Contact Book setup, define the Contact entity, stable identifier, validation rules, repository contract, and application-service boundary first. The console UI should call services rather than mutate domain objects directly.

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 1 (Setup + Core Structure)

Core syntax or pattern

csharp
class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

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

    public void AddContact(Contact c)
    {
        contacts.Add(c);
        Console.WriteLine("Contact added: " + c.Name);
    }

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

class Program
{
    static void Main(string[] args)
    {
        ContactManager manager = new ContactManager();
        bool running = true;

        while (running)
        {
            Console.WriteLine("\n1. Add Contact  2. Show All  0. Exit");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                Console.Write("Phone: ");
                string phone = Console.ReadLine();
                Console.Write("Email: ");
                string email = Console.ReadLine();
                manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
            }
            else if (choice == "2")
            {
                manager.ShowAll();
            }
            else if (choice == "0")
            {
                running = false;
            }
        }
    }
}

For Contact Book setup, define the Contact entity, stable identifier, validation rules, repository contract, and application-service boundary first. The console UI should call services rather than mutate domain objects directly.

Read the code step by step

  1. project requirementsclass Contact

    In this project we'll build a simple Contact Book console app that stores and lets you edit contacts (name, phone, email). We'll reuse the class, object, property, and constructor concepts from the basics chapter to learn how to model a real-world data structure. In Part 1, we'll start by writing the foundation: a Contact class, and a ContactManager class that stores contacts in a List. Get this structure solid, and adding more features in later parts will be a breeze. We'll also handle user input with Console.ReadLine() and build out the basic Add Contact feature.

  2. components, state, and servicesContactManager manager = new ContactManager();

    For Contact Book setup, define the Contact entity, stable identifier, validation rules, repository contract, and application-service boundary first. The console UI should call services rather than mutate domain objects directly.

  3. tested UI}

    Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

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 1 (Setup + Core Structure)

Failure

Allowing invalid names or phone values to enter objects anywhere scatters validation across the application.

Let's think this through for a moment

In this project we'll build a simple Contact Book console app that stores and lets you edit contacts (name, phone, email). We'll reuse the class, object, property, and constructor concepts from the basics chapter to learn how to model a real-world data structure. In Part 1, we'll start by writing the foundation: a Contact class, and a ContactManager class that stores contacts in a List. Get this structure solid, and adding more features in later parts will be a breeze. We'll also handle user input with Console.ReadLine() and build out the basic Add Contact feature.

Let's actually build it

Create a new Console App project, then write a Contact class in Contact.cs with Name, Phone, and Email properties. In the ContactManager class, add a List<Contact> field along with an AddContact() method and a ShowAll() method. In Main(), build a simple menu loop (while loop): when the user picks "1. Add Contact", read the name/phone/email with Console.ReadLine() and call AddContact(); when they pick "2. Show All", call ShowAll() to print the contacts list to the console. Add a condition for "0. Exit" so the loop can end.

Code example

csharp
class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

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

    public void AddContact(Contact c)
    {
        contacts.Add(c);
        Console.WriteLine("Contact added: " + c.Name);
    }

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

class Program
{
    static void Main(string[] args)
    {
        ContactManager manager = new ContactManager();
        bool running = true;

        while (running)
        {
            Console.WriteLine("\n1. Add Contact  2. Show All  0. Exit");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                Console.Write("Phone: ");
                string phone = Console.ReadLine();
                Console.Write("Email: ");
                string email = Console.ReadLine();
                manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
            }
            else if (choice == "2")
            {
                manager.ShowAll();
            }
            else if (choice == "0")
            {
                running = false;
            }
        }
    }
}
You should see
You'll end up with a working console app where a menu appears, you can add contacts, and view them back as a list.

5-minute challenge

Add a Category property ("Family", "Work", "Friend") to the Contact class, and show the category in ShowAll() too.

A quick word of caution

Make sure your exit condition in the menu loop is actually correct — get it wrong and you'll end up with an infinite loop.

Real-world usage

How to apply it in a real project

Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

Mini challenge

Build it yourself

Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
csharp
class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

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

    public void AddContact(Contact c)
    {
        contacts.Add(c);
        Console.WriteLine("Contact added: " + c.Name);
    }

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

class Program
{
    static void Main(string[] args)
    {
        ContactManager manager = new ContactManager();
        bool running = true;

        while (running)
        {
            Console.WriteLine("\n1. Add Contact  2. Show All  0. Exit");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                Console.Write("Phone: ");
                string phone = Console.ReadLine();
                Console.Write("Email: ");
                string email = Console.ReadLine();
                manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
            }
            else if (choice == "2")
            {
                manager.ShowAll();
            }
            else if (choice == "0")
            {
                running = false;
            }
        }
    }
}

Expected: Explain the core concept and runtime behavior of Project: Contact Book – Part 1 (Setup + Core Structure)

Show hint

Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

Quick quiz 1 / 3

What best describes the core idea of Project: Contact Book – Part 1 (Setup + Core Structure)?

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
  • For Contact Book setup, define the Contact entity, stable identifier, validation rules, repository contract, and application-service boundary first. The console UI should call services rather than mutate domain objects directly.
  • Allowing invalid names or phone values to enter objects anywhere scatters validation across the application.
  • Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

Unit testing C# in .NET — Microsoft Learn

Easy traps

  • Allowing invalid names or phone values to enter objects anywhere scatters validation across the application.
  • Assuming one successful happy path proves the code is correct, maintainable, and production-ready.

Hands-on Exercise

Split the solution into App, Core, and Tests projects and run one add-and-list vertical slice with an in-memory repository.

You'll know it worked when: You'll end up with a working console app where a menu appears, you can add contacts, and view them back as a list.

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 1 (Setup + Core Structure) | Thuta Learning