title: How to TryParse for Enum value?
body: <p>I want to write a function which can validate a given value (passed as a string) against possible values of an <code>enum</code>. In the case of a match, it should return the enum instance; otherwise, it should return a default value.</p>
  
  <p>The function may not internally use <code>try</code>/<code>catch</code>, which excludes using <code>Enum.Parse</code>, which throws an exception when given an invalid argument.</p>
  
  <p>I'd like to use something along the lines of a <code>TryParse</code> function to implement this:</p>
  
  <pre><code>public static TEnum ToEnum&lt;TEnum&gt;(this string strEnumValue, TEnum defaultValue)
  {
     object enumValue;
     if (!TryParse (typeof (TEnum), strEnumValue, out enumValue))
     {
         return defaultValue;
     }
     return (TEnum) enumValue;
  }
  </code></pre>