This might get downvoted, but this question has been bothering me since yesterday.. until I found a link then I knew I wasn’t really crazy lol:
Enum as instance variables
I’m basically asking the opposite of the OP’s question. Given:
enum Coffee {
BIG,
SMALL }
public class MyClass {
private Coffee coffee;
// Constructor etc.
}
Although this is Java and enums do differ somewhat in both languages how is it that
I can’t do coffee.BIG or coffee.BIG.SMALL (though it makes little sense when reading it, it should be possible considering coffee is of type Coffee) in C#?
This isn’t really to do with enums. It’s to do with accessing static members in general.
Java has a design flaw (IMO) which allows you to access static members as if they were instance members, via an expression of that type. It can lead to very confusing code:
Additionally, there’s no nullity check as the value of the expression is irrelevant:
Now, consider that enum values are implicitly static, and you can see why there’s the difference.
The fact that you’ve acknowledged that “it makes little sense when reading it” suggests that at heart you agree that this is a flaw within Java, not within C# 🙂