-1

I add four elements in a Dictionary, and iterate trough the dictionary with a ForEach loop : items are in the order of adding.

I add four elements in a ConcurentDictionary, and iterate trough the ConcurentDictionnary with a ForEach loop : items are NOT in the order of adding.

Why ?

    static void Main(string[] args)
    {

        Console.WriteLine("Add four items to dictionnary");
        var d = new Dictionary<decimal, int>();

        d.Add(1.05m, 1);
        d.Add(2.3m, 2);
        d.Add(1.3m, 3);
        d.Add(4m, 4);

        Console.WriteLine("iterate trough Dictionnary");
        foreach (var x in d) Console.WriteLine($"{x.Key:0.000} {x.Value}");

        Console.WriteLine("\r\nAdd four items to ConcurrentDictionary");
        var cd = new ConcurrentDictionary<decimal,int>();

        cd.TryAdd(1.05m, 1);
        cd.TryAdd(2.3m, 2);
        cd.TryAdd(1.3m, 3);
        cd.TryAdd(4m, 4);

        Console.WriteLine("iterate trough  ConcurrentDictionary");
        foreach (var x in cd) Console.WriteLine($"{x.Key:0.000} {x.Value}");

        Console.WriteLine("\r\niterate trough  Keys");
        foreach (var x in cd.Keys) Console.WriteLine($"{x:0.000}");

        Console.WriteLine("\r\niterate trough  Keys");
        foreach (var x in cd.Values) Console.WriteLine($"{x}");

        Console.ReadKey();
    }

Result

Add four items to dictionnary iterate trough Dictionnary
1.050 1
2.300 2
1.300 3
4.000 4

Add four items to ConcurrentDictionary
iterate trough ConcurrentDictionary
4.000 4
2.300 2
1.300 3
1.050 1

iterate trough Keys
4.000
2.300
1.300
1.050

iterate trough Values
4
2
3
1

edit : the test shows that even if the order is not guaranteed in the documentation, in fact it is.

I read the source in ConcurrentDictionary.cs, and I found why ConcurrentDictionary is different from Dictionary.

I try to write a response ( am french and beginner to write a question about stackoverflow)

Artibrico
  • 13
  • 3
  • 10
    Dictionaries are unordered. – SLaks Jan 24 '19 at 17:19
  • hum, I can not add an answer to my question anymore? – Artibrico Jan 24 '19 at 18:25
  • In fact, After reading the source code here: https://referencesource.microsoft.com ConcurrentDictionnary analyzes the type of the key with: IsValueWriteAtomic () - If the key can be written in an atomic way, ConcurrentDictionnay uses a table to store the key / value pairs. In this case, the values ​​are stored in the order of adding. - If the key can not be written at random, ConcurrentDictionary uses a linked list to store the key / value pairs. In this case, the storage order is not related to the order of adding. – Artibrico Jan 24 '19 at 18:26

1 Answers1

0

The MSDN documentation for a Dictionary (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2) states that

The order in which the items are returned is undefined

Although the documentation for a ConcurrentDictionary doesn't say so (https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2), the same is true for this too. This question Is the list order of a ConcurrentDictionary guaranteed? also states this to be the case.

If you want a sorted dictionary, there is a SortedDictionary class, but it is not Concurrent - if you want this, you'll have to write your own, or use a different approach for your code.

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92
  • Ok, what is strange, it is that in my tests, the problem arises only on the CnncurrentDictionnary with a key of type decimal ... – Artibrico Jan 24 '19 at 17:49