tags:

views:

449

answers:

6

Say I have enum as follows (taken from MSDN example):

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

I can then use is in the code as follows:

int today = (int)Days.Sun;

Question:

Can I evaluate enums? Say I have a variable Day whose value is "Sun". Will Days.Day evaluate to Days.Sun?

Thanks!

Please don't get hung up on the days example and tell me I should use comparison etc...i am looking to replace the usage of arrays with enums as it is more compact. So what I am looking for is essentially an ability to retrieve values from enum similar to an array:

myArray[Day] where day will evaluate to array key....

+3  A: 

Yes, you can do that. You can freely cast between an enum and its underlying type.

using System;

class Program
{
    static void Main()
    {
     // prints "zero"
     Console.WriteLine((Foo)0);
     // prints "1"
     Console.WriteLine((int)Foo.one);
    }
}

enum Foo { zero, one, two };
Andrew Hare
+7  A: 

Am I right in saying you've got:

string day = "Sun";

and you want to get back the value Days.Sun?

If so, use:

string day = "Sun";
Day day = (Day) Enum.Parse(typeof(Days), day);
Tim Robinson
+1 This is the correct way to parse and enum from a string.
Andrew Hare
+1 This is the correct way to parse an enum from a string.
Andrew Hare
Ahh!! Sorry about the double comment - I tried to fix a typo by commenting and then deleting but SO is not letting me delete the first one. I would have upvoted twice if I could :)
Andrew Hare
Beat me to it, good job Tim.
Andrew Burns
Thanks! this helps!
gnomixa
A: 

for int yes , but for string you should do something like this

Adinochestva
+1  A: 

You can also parse the string values as well. Using Enum.Parse

[TestFixture]
public class EnumParsingSO
{
 public enum Days
 {
  Sat = 1,
  Sun,
  Mon,
  Tues
 }

 [Test]
 public void EnumFromString()
 {
  Days expected = Days.Mon;
  int expectedInt = 3;
  Days actual = (Days)Enum.Parse(typeof(Days), "Mon");

  Assert.AreEqual(expected, actual);
  Assert.AreEqual(expectedInt, (int)actual);
 }
}
Andrew Burns
+1  A: 

As Andrew says, you can cast from an enum to its underlying type. You can also unbox between the two as well, and there's an implicit conversion from the constant 0 to any enum type:

using System;

enum Demo { Foo, Bar, Baz };

class Test
{
    static void Main()
    {
        int i = 2;
        object o = i;
        // Unboxing from int to Demo
        Demo d = (Demo) o;
        Console.WriteLine(d);

        o = Demo.Baz;
        // Unboxing from Demo to int
        i = (int) o;
        Console.WriteLine(i);

        // Implicit conversion of 0
        d = 0;
        Console.WriteLine(d);
    }
}
Jon Skeet
The question is simpler than that I believe. His day variable is already of the enum type.
Noldorin
+1 This is a good thing to point out.
Andrew Hare
Ok, that's *another* bug with comments I've encountered today. I sware I posted this on Tim Robinson's answer.
Noldorin
A: 

Yes you can, you just need to provide the native integer value for each enum value. From your example:

enum Days {
    Sat=1, 
    Sun=2, 
    Mon=3, 
    Tue=4, 
    Wed=5, 
    Thu=6, 
    Fri=7
};

(Now, why you would order days of the week in this way is beyond me...)

Adam
Although I agree that you /should/ assign numbers to each value, it is not required.
Andrew Burns
guys! its just an example! :)
gnomixa