Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we access enum values from an enum instance in C#?

Tags:

java

c#

enums

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

like image 446
Lews Therin Avatar asked Aug 12 '12 08:08

Lews Therin


People also ask

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

Can enum have instance variables?

Enums are very powerful as they may have instance variables, instance methods, and constructors. Each enum constant should be written in capital letters. Every enum constant is by default internally public static final of type Enum declared.

How do you find the value of an enum?

Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.


2 Answers

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:

Thread thread = new Thread(...);
thread.start();
// This looks like it makes the new thread sleep, but it actually makes the
// current thread sleep
thread.sleep(1000);

Additionally, there's no nullity check as the value of the expression is irrelevant:

Thread thread = null;
thread.sleep(1000); // No exception

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# :)

like image 110
Jon Skeet Avatar answered Oct 30 '22 05:10

Jon Skeet


In C# (unlike Java) it is not legal to access a static field via an instance of that class.

If you write this:

coffee.BIG.SMALL

Then the error you get is:

Member 'coffee.SMALL' cannot be accessed with an instance reference; qualify it with a type name instead

This code also won't work for the same reason:

void Foo(coffee c)
{
    // Member 'coffee.SMALL' cannot be accessed with an instance reference
    Console.WriteLine(c.SMALL); 
}
like image 32
Mark Byers Avatar answered Oct 30 '22 07:10

Mark Byers