-4

I have an Enum

public enum MyEnum
    {
        value1= 1,
        value2= 2,
        value3= 3,
        value4= 4
    }

How do I get the integer values , if I give the string as input , say input--> value2 , then , output-->2

Thanks

Roby A
  • 38
  • 8
  • 1
    See [Enumeration types (C# reference) - Conversions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum#conversions) – Fildor Oct 01 '20 at 06:16

1 Answers1

2

Basically you cam use Enum.Parse to parse the string and then cast to int like below :

var str  = "value1";
var value = (int)Enum.Parse<MyEnum>(str);

Fiddle

Eldar
  • 9,781
  • 2
  • 10
  • 35