I am not an expert in C # and LINQ.
I have a Dictionary , which I understand hash table, that is, keys are not sorted.
dataBase = new Dictionary<string, Record>()
Record is a custom class that contains a series of data for a given key string.
I found an interesting example that converts this Dictionary into a sorted dictionary by LINQ:
var sortedDict = (from entry in dataBase orderby entry.Key ascending select entry) .ToDictionary(pair => pair.Key, pair => pair.Value);
This code is working correctly. The resulting sortedDict sorted by key.
Question : I found that sortedDict is still a hash table, type:
System.Collections.Generic.Dictionary<string, Record>
I expected that the resulting dictionary should be a kind of map , as in C ++ STL, which is usually implemented as a (balanced) binary tree to maintain key order. However, the resulting dictionary is still a hash table.
How can sortedDict maintain order? A hash table cannot hold key order. Is the C # Generic.Dictionary different from a typical hash table?
dictionary hashtable c #
minjang
source share