Creating an Application Using C# Dictionary

Welcome! Today, we will explore creating a simple address book application using a C# Dictionary. This task will help you understand how to manipulate a Dictionary in C#, focusing on adding, retrieving, and deleting entries. By the end of this lesson, you'll have a solid grasp of these fundamental operations.

Introducing Methods to Implement

In this task, we will implement three methods to manage our address book:

  • AddContact(string name, string phoneNumber): Adds a new contact. Returns false if the contact already exists; otherwise, it adds the contact and returns true. In this task, let's assume phone numbers do not change, so it's not allowed to overwrite an existing contact's number.
  • GetContact(string name): Retrieves the phone number for a given name. Returns null if the contact does not exist.
  • DeleteContact(string name): Deletes a contact with the given name. Returns true if the contact exists and is deleted, false otherwise.

Let's break down each method in detail in the next sections.

Step 1: Implementing AddContact

This method adds a new contact to the address book with the given name and phoneNumber. If the contact already exists, it returns false. Otherwise, it adds the contact and returns true.

Question: Why do you think we need to check if the contact already exists?

Answer: To avoid duplicating existing entries. Also, if a contact with the same name already exists, we shouldn't allow overwriting its phone number in this method, as it's only for creation.

Here is the method implementation:

In this method:

  • We verify if the contact already exists using if (contacts.ContainsKey(name)).
  • If it exists, we return false.
  • If it doesn't exist, we add it to our Dictionary using contacts.Add(name, phoneNumber); and return true.
Step 2: Implementing GetContact

This method retrieves the phone number associated with a given name. If the contact does not exist, it returns null.

Question: What do we gain by returning null when a contact doesn't exist?

Answer: It provides a clear indicator that the contact is not in the address book, allowing us to handle such cases gracefully.

Here is the method implementation:

In this method:

  • We use contacts.TryGetValue(name, out string phoneNumber);, which tries to get the value for the key name. If the key exists, it returns the phone number; otherwise, it returns null.
Step 3: Implementing DeleteContact

This method deletes a contact with the given name. If the contact exists and is deleted, it returns true. If the contact does not exist, it returns false.

Question: What could be a real-world consequence of not checking if the contact exists before deletion?

Answer: One consequence can be assuming that a contact was removed when, in fact, it was never there, leading to inaccurate tracking of records.

Here is the method implementation:

In this method:

  • We use return contacts.Remove(name);, which removes the entry if the key exists and returns true. If the key is not found, it returns false.
Summary

In this lesson, we explored creating a simple address book application using a C# Dictionary. We implemented three key methods: AddContact, GetContact, and DeleteContact to manage contacts effectively by focusing on adding, retrieving, and deleting entries. With the benefits of Dictionaries, such as efficient operations, flexible key usage, and automatic handling of unique keys, we've demonstrated how to construct a robust address book application. Great work! Now, let's proceed to the practice exercises to reinforce your understanding.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal