开发者

Dictionary Casting

开发者 https://www.devze.com 2023-01-25 17:20 出处:网络
Is it possible to cast an enumerated-key dictionary to an integer-key dictionary? (Or a copy constructor would work fine for what I\'m doing as well.) In other words, something that looks like:

Is it possible to cast an enumerated-key dictionary to an integer-key dictionary? (Or a copy constructor would work fine for what I'm doing as well.) In other words, something that looks like:

Dictionary<int, string> NewDictionary =
    (Dictionary<int, string>)OldDictionary<My开发者_开发技巧Enum, string>;

(Yes, I know that syntax isn't quite correct, it's just to show what it is I'm coming from.)


You could iterate over the dictionary and build a new one, transforming each key (e.g. by using the Enumerable.ToDictionary extension) :

OldDictionary.ToDictionary(x => ((int)x.Key, x => x.Value);
0

精彩评论

暂无评论...
验证码 换一张
取 消