How to create a simple phone book with dictionary in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
 
namespace ConsoleApplication1
{
    public delegate void mydelegate();

    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, long> phonebook = new Dictionary<string, long>();

            phonebook.Add("Tom", 1234567);
            phonebook.Add("Emma", 9865764);

            phonebook["Ava"] = 6487291;

            foreach (KeyValuePair<string, long> keyval in phonebook)
            {
                Console.WriteLine("{0} - {1}", keyval.Key, keyval.Value);
            }

            if (phonebook.ContainsKey("Ava"))
            {
                Console.WriteLine("\nAva phone number: " + phonebook["Ava"]);
            }
            Console.WriteLine("\nTotal numbers: " + phonebook.Count);

            phonebook.Remove("Ava");
            Console.WriteLine("\nAfter Remove - Total numbers: " + phonebook.Count);
            foreach (KeyValuePair<string, long> keyval in phonebook)
            {
                Console.WriteLine("{0} - {1}", keyval.Key, keyval.Value);
            }
        }
    }
}

/*
run:
     
Tom - 1234567
Emma - 9865764
Ava - 6487291

Ava phone number: 6487291

Total numbers: 3

After Remove - Total numbers: 2
Tom - 1234567
Emma - 9865764
        
*/

 



answered Apr 24, 2017 by avibootz
edited Apr 24, 2017 by avibootz

Related questions

...