Skip to content

Dictionary keys printing problem

Javed Iqbal

Javed Iqbal

Asked Sep 14, 2014, 11:47 PM EDT1 answer
Share
LinkedInX
Why is this code printing the same 'key' once only?
C#
  Dictionary<int,string> dictionary = new Dictionary<int,string>();
          var keys=new List<string>();
          try
          {
              dictionary.Add(5, "javed");
              dictionary.Add(5, "khan");
              dictionary.Add(5, "javed");


          }
          catch (ArgumentException)
          {
              Console.WriteLine("Key/Value pair already used");


          }


          finally
          {


              foreach (int key in dictionary.Keys)
              {
                  Console.WriteLine(key);


              }






          }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 1 short reply
    • Javed Iqbal
      0
      Output:
      5
      0