C# - Phonebook Program

Learn, how to create / design a phonebook application using C# program?
Submitted by Nidhi, on August 20, 2020 [Last updated : March 22, 2023]

Creating a phonebook application

Here, we will create a C# program to create a phonebook using HashTable, here we add numbers to the Phonebook and get numbers from the phone book.

C# program to implement phonebook

The source code to create a phonebook in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to implement phonebook in C#.
using System;
using System.Collections;
class program {
  public static void Main() {
    Hashtable phoneBook = new Hashtable();

    while (true) {
      Console.WriteLine("\n#############PhoneBook###########");
      Console.WriteLine("\t1:Add number to phone book");
      Console.WriteLine("\t2:Get number to phone book");
      Console.WriteLine("\t3:Exit");
      Console.WriteLine("\n#################################");
      Console.WriteLine("\n\nEnter choice: ");
      int choice = Convert.ToInt32(Console.ReadLine());

      switch (choice) {
      case 1:
        {
          long number = 0;
          string name = "";

          Console.Write("Enter your name : ");
          name = Console.ReadLine();

          Console.Write("Enter your phone number : ");
          number = Convert.ToInt64(Console.ReadLine());

          phoneBook.Add(name, number);
        }
        break;
      case 2:
        {
          long number = 0;
          string name = "";

          Console.Write("Enter your name : ");
          name = Console.ReadLine();

          if (phoneBook[name] == null) {
            Console.WriteLine("Given name is not found in phonebook");
          }
          else {
            number = Convert.ToInt64(phoneBook[name]);
            Console.WriteLine("Name: " + name + ", phone number: " + number);
          }
        }
        break;
      case 3:
        {
          goto OUT;
        }
        break;
      default:
        {
          Console.WriteLine("\nYou have entered wrong choice");
        }
        break;
      }
    }

    OUT:
    Console.WriteLine("\nThankyou for using phonebook");
  }
}

Output

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################


Enter choice:
1
Enter your name : Arvind
Enter your phone number : 8860572892

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################

Enter choice:
2
Enter your name : Arvind
Name: Arvind, phone number: 8860572892

#############PhoneBook###########
        1:Add number to phone book
        2:Get number to phone book
        3:Exit
#################################

Enter choice:
3

Thankyou for using phonebook
Press any key to continue . . .

Explanation

In the above program, we created a phonebook using HashTable. Here we provide two options 1st to add name and phone number to the phone book and 2nd option is used to getting numbers from the phonebook.

As we know that we can store items in the hash table in the form key/value pair. Here we used the name as a key and phone number as the value in the phonebook hash table.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.