c# get enum name code example
Example 1: get enum by index c#
(EnumType)int;
(EnumType)int.ToString();
Example 2: get enum name from value c#
int enumValue = 2;
string enumName = Enum.GetName(typeof(EnumDisplayStatus), enumValue);
Example 3: c# get enum value from string
Keys key = (Keys)Enum.Parse(typeof(Keys), "Space");
Example 4: c# get enum name from value
string name=(weekdays)2;
Example 5: c# get string value of enum
using System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static void Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
}
}
Example 6: get key from c# enum for specific value
enum myEnum { firstValue: 1, secondValue 2, thirdValue = 3};
string text = Enum.GetName(typeof(myEnum), 2);