开发者

Dictionary Casting

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜