enum from string c# code example

Example 1: string to enum c# 3

StatusEnum MyStatus = (StatusEnum) Enum.Parse(typeof(StatusEnum), "Active", true);

Example 2: string to enum c#

Enum.TryParse("Active", out StatusEnum myStatus);

Example 3: C# .net core convert string to enum

var foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
if (Enum.IsDefined(typeof(YourEnum), foo))
{
    return foo;
}

Example 4: c# string enum

public static class Status
{
    public const string Awesome = "Awesome";
    public const string Cool = "Cool";
}
//Not an enum but has a similar effect without needing to convert ints