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
}
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\";
}
}
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) { ... }
}
π‘ 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...
}
}
}
π 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):
π 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)
Awesome! Always want to build something like that! Maybe I could build something like it in Godot?