Search
Learn
Share

Convert string to Enum in C#

Published by Profile Image DevDashboard on 9 September, 2017
17 views
Use this extension method to make it simple to convert a string to an Enum

public static T ToEnum<T>(this string value, bool ignoreCase = true)
{
try
{
return (T)Enum.Parse(typeof(T), value, ignoreCase);
}
catch (Exception)
{
return default(T);
}
}