4
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?

1
  • Although question has been answered but would like to tell one mistake that when function a is static then how it can assign the value 3 to non static member x. Sorry for going out of the scope of this questions but I couldn't stop myself for taking a worth note on that line of code. I hope you won't mind. :) Commented Sep 18, 2010 at 18:00

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

"10.4:Even though constants are considered static members, a constant-declaration neither requires nor allows a static modifier." combined with 3.4.3 makes a satisfying answer.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.