Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

C#: 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;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #casting #string #enum #object
ADD COMMENT
Topic
Name
4+3 =