string to enum csharp

Code Example - 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;
}
                            
                        
 

csharp string to enum

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

csharp convert string to enum

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

csharp string enum

                        
                                public class LogCategory
{
    private LogCategory(string value) { Value = value; }

    public string Value { get; set; }

    public static LogCategory Trace   { get { return new LogCategory("Trace"); } }
    public static LogCategory Debug   { get { return new LogCategory("Debug"); } }
    public static LogCategory Info    { get { return new LogCategory("Info"); } }
    public static LogCategory Warning { get { return new LogCategory("Warning"); } }
    public static LogCategory Error   { get { return new LogCategory("Error"); } }
}