如何将sortedDictionary转换为字典?

Ujw*_*ire 6 c# generics

Dictionary<string, string> optionDictionary = new Dictionary<string, string>();

optionDictionary = ....;

SortedDictionary<string, string> optionsSorted;

if(sorting)
{
   optionsSorted = new SortedDictionary<string, string>(optionDictionary );
   // Convert SortedDictionary into Dictionary
}

return optionDictionary ;
Run Code Online (Sandbox Code Playgroud)

BFr*_*ree 15

您可以将optionsSorted<TKey,TValue>字典作为参数传递给新实例,Dictionary<TKey,TValue>如下所示:

var dictionary = new Dictionary<type1,type2>(optionsSorted);
Run Code Online (Sandbox Code Playgroud)


Jar*_*Par 11

请尝试以下方法

var dictionary = optionsSorted.ToDictionary(x => x.Key, x=> x.Value);
Run Code Online (Sandbox Code Playgroud)