Natrium
Natrium

Reputation: 31204

How to retrieve the integer value of an enum?

I have an enum

public enum Color
{
    Red = 0,
    Blue = 1,
    Yellow = 2
}

When I do this:

Color color = Color.Blue;
Console.Writeline(color.Value);

I want to see it's integer-value (1 in this case), but it outputs "Blue" instead.

How can I solve this?

I use .NET 3.5.

Upvotes: 2

Views: 224

Answers (3)

KhanS
KhanS

Reputation: 1195

int value = Convert.ToInt32(Color.Blue);

Upvotes: 3

Yongwei Xing
Yongwei Xing

Reputation: 13451

Enum.Parse(typeof(Color), "Blue", true);

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838984

You can cast to int:

Console.Writeline((int)color.Value);

Upvotes: 10

Related Questions