public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//枚举类型
enum Season
{
spring=1,summer,fall,autumn=fall,winter
}
private void Form1_Load(object sender, EventArgs e)
{
Season k = Season.summer;
MessageBox.Show(k.ToString());
object o = k;
int? i = 9;
i = o as int?;
if (o == null)
{
MessageBox.Show("fail");
}
else
{
MessageBox.Show(i.ToString());
}
//int i = (int)k;
//MessageBox.Show(i.ToString());
}
}
因为i 是null
o as int? 因为o 不是int?类型 , o as int? 就返回null
如下:as转换成功为什么不能显示0?
没有证据显示as转换成功了。as转换永远都不会抛出异常,如果失败那么转换结果为null。