示例代码:
public enum Foods { Burger,Pizza,Cake } private void button1_Click(object sender,EventArgs e) { Eat(0); // A Eat((Foods)0); // B //Eat(1); // C : won't compile : cannot convert from 'int' to 'Foods' Eat((Foods)1); // D } private void Eat(Foods food) { MessageBox.Show("eating : " + food); }
解决方法
是的,文字0可隐式转换为任何枚举类型,并表示该类型的默认值.根据C#语言规范,特别是关于枚举的1.10节:
The default value of any enum type is the integral value zero converted to the enum type. In cases where variables are automatically initialized to a default value,this is the value given to variables of enum types. In order for the default value of an enum type to be easily available,the literal 0 implicitly converts to any enum type. For the default value of an enum type to be easily available,the literal 0 implicitly converts to any enum type.
原文地址:https://www.jb51.cc/csharp/95849.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。