How to get the integer value of an enumeration?

I have an enumeration

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 its integer value (in this case 1), but instead displays "Blue".

How can i solve this?

I am using .NET 3.5.

+3
source share
3 answers

You can use for int:

Console.Writeline((int)color.Value);
+10
source
int value = Convert.ToInt32(Color.Blue);
+3
source
Enum.Parse(typeof(Color), "Blue", true);
0
source

Source: https://habr.com/ru/post/1740885/


All Articles