public enum sEnum
{
zero = 0, one = 1
}
public int x;
public static void a(sEnum s)
{
x = 3;
if (s == sEnum.one) ...
}
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.
Section 3.4.3 of the language specification states:
The members of an enumeration are the constants declared in the enumeration
I think 14.3 in the specs is what you are looking for:
Enum members are named and scoped in a manner exactly analogous to fields within classes. The scope of an enum member is the body of its containing enum type. Within that scope, enum members can be referred to by their simple name. From all other code, the name of an enum member must be qualified with the name of its enum type. Enum members do not have any declared accessibility—an enum member is accessible if its containing enum type is accessible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With