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)