Answer to Question #189413 in C# for CHANDRASENA REDDY

Question #189413

You need to maintain file extensions along with file types in dictionary class

Tasks:

• Create a new dictionary of strings, with string keys.

• Add some elements to dictionary. There should not be duplicate keys, but some of values can be duplicates.

• The Add method throws an exception if new key is already in dictionary. Test this by adding a duplicate key.

• The indexer can be used to change value associated with a key. Try changing the value of any record and display the updated value.

• If a key does not exist, setting the indexer for that key adds a new key/value pair. Try this by adding a new value.

• The indexer throws an exception if requested key is not in dictionary. Try printing any such key which is not present and handle exception.

• When you use foreach to enumerate dictionary elements, the elements are retrieved as Key/Value Pair objects. Use a foreach loop to print the values and test this.

• Use the Remove method to remove a key/value pair.


1
Expert's answer
2021-05-14T01:21:55-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;


namespace C_SHARP
{
    class Program
    {


        /// <summary>
        /// The Add method throws an exception if new key is already in dictionary. Test this by adding a duplicate key.
        /// </summary>
        /// <param name="countries"></param>
        /// <param name="country"></param>
        /// <returns></returns>
        static bool keyIsAlreadyDictionary(Dictionary<string, string>  countries, string country)
        {
            if (countries.ContainsKey(country))
            {
                throw new ArgumentException("This key \"" + country + "\" is already in dictionary");
            }
            return false;
        }


        static void Main()
        {
            //Create a new dictionary of strings, with string keys.
            Dictionary<string, string> countries = new Dictionary<string, string>();
            //Add some elements to dictionary. There should not be duplicate keys, but some of values can be duplicates.
            countries.Add("Italy", "Rome");
            countries.Add("India", "New Delhi");
            countries.Add("China", "Peking ");
            countries.Add("Chile", "Santiago");
            countries.Add("USA", "Washington");
            countries.Add("Ukraine", "Kiev");
            countries.Add("Turkey", "Ankara");
            countries.Add("Zimbabwe", "Harare");
            countries.Add("Thailand", "Bangkok");
            countries.Add("South Korea", "Seoul");
            countries.Add("Serbia", "Belgrade");
            countries.Add("Pakistan", "Islamabad");
            try
            {
                if (!keyIsAlreadyDictionary(countries, "Pakistan"))
                {
                    countries.Add("Pakistan", "Islamabad");
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            
            // The indexer can be used to change value associated with a key. Try changing the value of any record and display the updated value.
            if (countries.ContainsKey("Serbia"))
            {
                countries["Serbia"] = "New Serbia";
                Console.WriteLine("The capital of country \"Serbia\" has been changed. A new capital is \"" + countries["Serbia"] + "\"\n");


            }
            // If a key does not exist, setting the indexer for that key adds a new key/value pair. Try this by adding a new value.
            try
            {
                if (!keyIsAlreadyDictionary(countries, "Poland"))
                {
                    countries.Add("Poland", "Warsaw");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
          
            // When you use foreach to enumerate dictionary elements, the elements are retrieved as Key/Value Pair objects. 
            //Use a foreach loop to print the values and test this.
            Console.WriteLine();
            foreach (string key in countries.Keys)
            {
                Console.WriteLine(string.Format("{0,-20} {1,-20}", key, countries[key]));
            }
            // Use the Remove method to remove a key/value pair.
            Console.WriteLine();
            if (countries.ContainsKey("Chile"))
            {
                countries.Remove("Chile");
                Console.WriteLine("The entry for \"Chile\" has been deleted.");
            }
            else
            {
                Console.WriteLine("The dictionary does not contain the key \"Chile\"");
            }
            Console.WriteLine();
            foreach (string key in countries.Keys)
            {
                Console.WriteLine(string.Format("{0,-20} {1,-20}", key, countries[key]));
            }


            // The indexer throws an exception if requested key is not in dictionary. Try printing any such key which is not present and handle exception.
            try
            {
                Console.WriteLine(countries["Serbia 1"]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS