2

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.

2
  • @nalka This a 14 year old question, and the question you are refeering to is 15 years old. Does this really need to happen? Commented Oct 9, 2024 at 7:05
  • 1
    It's just the automatic comment when flagging as duplicate Commented Oct 9, 2024 at 8:09

3 Answers 3

10

You can cast to int:

Console.Writeline((int)color.Value);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

1 Comment

The OP wants to get the integer value of a particular enum value, not convert some string into an enum value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.