How does Dictionary in C# add Key/Value?

Shervan360 1,661 Reputation points
2024-01-17T01:02:56.2966667+00:00

Hello, I removed a key/value from the middle of the Dictionary and then added the same key/value to this. The key/value pair is added in the previous(middle) position. (I expected new key/value pair added to begin or last) How does Dictionary in C# add Key/Value? Screenshot 2024-01-17 032936

// Example file for C# Applied Data Structures by Joe Marini
// Programming Challenge

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Challenge
{
     class Program
     {
          static void PrintDic(Dictionary<string, string> dic)
          {
               Console.WriteLine("Dic\t");
               foreach (var item in dic)
               {
                    Console.WriteLine($"{item.Key}\t{item.Value}");
               }
               Console.WriteLine();
          }
          static void PrintOrderedDic(OrderedDictionary dic)
          {
               Console.WriteLine("Ordered Dic\n");
               foreach (DictionaryEntry item in dic)
               {
                    Console.WriteLine($"{item.Key}\t{item.Value}");
               }
               Console.WriteLine();
          }
          static void Main(string[] args)
          {
               Dictionary<string, string> Dic = new();

               Dic.Add("Key1", "Value1");
               Dic.Add("Key2", "Value2");
               Dic.Add("Key5", "Value5");
               Dic.Add("Key4", "Value4");
               Dic.Add("Key3", "Value3");

               PrintDic(Dic);
               Dic.Remove("Key5");
               PrintDic(Dic);
               Dic.Add("Key5", "Value5");
               PrintDic(Dic);

               //OrderedDictionary orderedDic = new();

               //orderedDic.Add("Key1", "Value1");
               //orderedDic.Add("Key2", "Value2");
               //orderedDic.Add("Key5", "Value5");
               //orderedDic.Add("Key4", "Value4");
               //orderedDic.Add("Key3", "Value3");
               
               //PrintOrderedDic(orderedDic);
               //orderedDic.Remove("Key5");
               //PrintOrderedDic(orderedDic);
               //orderedDic.Add("Key5", "Value5");
               //PrintOrderedDic(orderedDic);
          }

     }
}

Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-01-17T01:13:26.41+00:00

    Hi @Shervan360 ,

    Dictionary<TKey, TValue> class does not guarantee any specific order of elements. The order in which elements are stored in the dictionary is not necessarily the order in which they were added.

    The Dictionary<TKey, TValue> class uses a hash table to store key/value pairs. The hash table organizes the elements based on the hash code of the keys. The hash code determines the position of the element in the internal data structure, providing a fast way to look up values based on their keys.

    When you add or remove items, the internal organization may change, and the new key/value pair may end up in a different position. This is why you observe that the new key/value pair is added in a different position than expected.

    If you need to maintain the order of elements based on their insertion order, you can use the OrderedDictionary Class. Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.