Accessing non-static enum values from static methods

后端 未结 2 810
离开以前
离开以前 2021-01-13 22:41
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

相关标签:
2条回答
  • 2021-01-13 23:01

    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.

    0 讨论(0)
  • 2021-01-13 23:22

    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

    0 讨论(0)
提交回复
热议问题