DEV Community

Kerimova_Manzura
Kerimova_Manzura

Posted on • Edited on

πŸ“’ Building a Simple Contact Book in C# – A Step-by-Step Guide

Managing contacts is a fundamental part of many applications. Whether you're organizing your friends, family, or work-related connections, having a simple system to store and manage contact information is essential.

In this tutorial, we’ll build a console-based Contact Book in C# from scratch. This project is perfect for beginners and covers important concepts such as:

βœ… Object-Oriented Programming (OOP)
βœ… Enums and Structs
βœ… Working with Lists
βœ… Implementing CRUD operations
βœ… Interactive Console UI


🧱 Application Overview

Our application will include the following key components:

  • enum ContactType β€” To classify the type of contact (Friend, Family, etc.)
  • struct Contact β€” To define the structure of a contact
  • class AddressBook β€” To manage the contact list and operations
  • Main method β€” A console-based UI to interact with users

Let’s break it down step-by-step.


1️⃣ Contact Type Enum

This enum helps categorize each contact into meaningful groups.

enum ContactType
{
    Friend,
    Family,
    Professional,
    Other
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ Contact Struct

We define each contact using a simple struct that includes the contact's name, address, phone number, and type.

struct Contact
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public ContactType Type { get; set; }

    public Contact(string name, string address, string phoneNumber, ContactType type)
    {
        Name = name;
        Address = address;
        PhoneNumber = phoneNumber;
        Type = type;
    }

    public override string ToString()
    {
        return $\"Name: {Name}\\nAddress: {Address}\\nPhone: {PhoneNumber}\\nType: {Type}\\n\";
    }
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ Address Book Class

This class manages a collection of contacts and provides methods for:

  • Adding new contacts
  • Editing existing contacts
  • Deleting contacts
  • Displaying all contacts
  • Filtering contacts by type
class AddressBook
{
    private List<Contact> contacts = new();

    public void AddContact(Contact contact) { ... }
    public void EditContact(string name, Contact updatedContact) { ... }
    public void DeleteContact(string name) { ... }
    public void ShowAllContacts() { ... }
    public void FilterContactsByType(ContactType type) { ... }
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Tip: We use List<Contact> for dynamic contact management and LINQ for filtering.


4️⃣ Console UI (Main Method)

We use a simple loop-driven console menu for user interaction. Here, users can perform all available operations step-by-step.

class Program
{
    static void Main()
    {
        AddressBook addressBook = new AddressBook();

        while (true)
        {
            Console.WriteLine(\"\\nπŸ“’ Contact Book Menu:\");
            Console.WriteLine(\"1. βž• Add Contact\");
            Console.WriteLine(\"2. πŸ“ƒ Show All Contacts\");
            Console.WriteLine(\"3. ✏️ Edit Contact\");
            Console.WriteLine(\"4. πŸ—‘οΈ Delete Contact\");
            Console.WriteLine(\"5. πŸ” Filter Contacts by Type\");
            Console.WriteLine(\"6. ❌ Exit\");
            Console.Write(\"Choose an option (1-6): \");

            string choice = Console.ReadLine();
            // Logic for each menu option goes here...
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Sample Output (Console UI)

πŸ“’ Contact Book Menu:
1. βž• Add Contact
2. πŸ“ƒ Show All Contacts
3. ✏️ Edit Contact
4. πŸ—‘οΈ Delete Contact
5. πŸ” Filter Contacts by Type
6. ❌ Exit
Choose an option (1-6):
Enter fullscreen mode Exit fullscreen mode

πŸš€ What You’ll Learn

By completing this project, you will gain practical experience with:

  • Structs and Enums
  • Collections (List<T>)
  • LINQ filtering
  • Input validation
  • Console application architecture

🌱 What’s Next?

Here are a few ideas to improve or expand the project:

  • πŸ“ Save contacts to a file (JSON or CSV)
  • πŸ” Add search functionality
  • πŸ–ΌοΈ Build a GUI version using Windows Forms or WPF
  • 🌐 Create a web version using ASP.NET

βœ… Final Thoughts

This project is an excellent way to get hands-on with the fundamentals of C# and real-world coding patterns. It’s simple enough for beginners but flexible enough to grow into something more powerful.

Ready to level up? Try building the same app with a GUI or save/load data to disk!

Top comments (1)

Collapse
 
chesedgamesonline profile image
ChesedGamesOnline

Awesome! Always want to build something like that! Maybe I could build something like it in Godot?