本文介绍了从静态方法访问非静态的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public enum sEnum
{
zero = 0, one = 1
}
public int x;
public static void a(sEnum s)
{
x = 3;
if (s == sEnum.one) ...
}
为什么能枚举值在这里检查,因为static关键字不使用?这哪里是在语言规范记录?
Why can values of the enum be checked here, as the static keyword isn't used? Where is this documented in the language specification?
推荐答案枚举刚刚命名的值,因此您可以在静态情况下,就像使用它们任何其他不变。
Enums are just named values so you can use them in a static context just like any other constant.
语言规范状态的第3.4.3节:
Section 3.4.3 of the language specification states:
的成员枚举是在枚举中声明的常量
The members of an enumeration are the constants declared in the enumeration
更多推荐
从静态方法访问非静态的枚举值
发布评论