Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a ResourceDictionary to a Dictionary in C#

I have a ResourceDictionary only containing string keys and string values. Now I want to have a Dictionary< string, string > with the same content.

How would you do that? Whats the fastest solution in C#?

Edit: Fastest in terms of perfomance ;)

like image 757
Christian Hubmann Avatar asked Jan 23 '23 17:01

Christian Hubmann


1 Answers

Fastest in terms of simplest? Assuming .NET 3.5 (and thus LINQ) I'd use:

resourceDictionary.Keys.Cast<string>().ToDictionary
    (x => x,                             // Key selector
     x => (string) resourceDictionary[x] // Value selector
     );
like image 56
Jon Skeet Avatar answered Jan 26 '23 06:01

Jon Skeet