csharp convert string to enum

Code Example - csharp convert string to enum

                
                        MyEnum enumValue = Enum.Parse<MyEnum>(stringValue);
                    
                
 

string to enum csharp

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

csharp .net core convert string to enum

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

csharp 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
                            
                        
 

csharp: casting string to enum object

                        
                                public static T ToEnum<T>(this string value, T defaultValue) 
{
    if (string.IsNullOrEmpty(value))
    {
        return defaultValue;
    }

    T result;
    return Enum.TryParse<T>(value, true, out result) ? result : defaultValue;
}