C# program to implement phonebook

Here, we are going to learn how to implement a phonebook using C# program?
Submitted by Nidhi, on August 20, 2020

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.

Program:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.