【发布时间】:2013-12-01 10:05:33
【问题描述】:
enum Gender { Male, Female }
var k = new[] { Gender.Male }.Cast<int>().ToList().Cast<int?>().ToList(); //alright
var p = new[] { Gender.Male }.Cast<int>().Cast<int?>().ToList(); //InvalidCastException
第二种情况的原因是什么?我知道我不能直接将盒装的enum 投射到int?,但我做了两个阶段的投射,即Cast<int>.Cast<int?>,这应该可以工作。
编辑:
考虑到以下作品,这令人惊讶:
object o = Gender.Male;
int i = (int)o; // so here the cast is not to an entirely different type, which works
【问题讨论】:
标签: c# casting enums type-conversion