c# get enum value from string code example
Example 1: C# .net core convert string to enum
var foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
if (Enum.IsDefined(typeof(YourEnum), foo))
{
return foo;
}
Example 2: c# get enum value from string
//This example will parse a string to a Keys value
Keys key = (Keys)Enum.Parse(typeof(Keys), "Space");
//The key value will now be Keys.Space
Example 3: c# get enum name from value
string name=(weekdays)2;//returns weekdays which has value 2.Here weekdays is enum name
Example 4: how to pass string value to enum in c#
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myString);
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));
}
}
// The example displays the following output:
// The 4th value of the Colors Enum is Yellow
// The 4th value of the Styles Enum is Corduroy
Example 6: get enum value c#
int something = (int) Question.Role;