title: C# non-boxing conversion of generic enum to int?
body: <p>Given a generic parameter TEnum which always will be an enum type, is there any way to cast from TEnum to int without boxing/unboxing?</p>
  
  <p>See this example code.  This will box/unbox the value unnecessarily.</p>
  
  <pre><code>private int Foo&lt;TEnum&gt;(TEnum value)
      where TEnum : struct  // C# does not allow enum constraint
  {
      return (int) (ValueType) value;
  }
  </code></pre>
  
  <p>The above C# is release-mode compiled to the following IL (note boxing and unboxing opcodes):</p>
  
  <pre><code>.method public hidebysig instance int32  Foo&lt;valuetype 
      .ctor ([mscorlib]System.ValueType) TEnum&gt;(!!TEnum 'value') cil managed
  {
    .maxstack  8
    IL_0000:  ldarg.1
    IL_0001:  box        !!TEnum
    IL_0006:  unbox.any  [mscorlib]System.Int32
    IL_000b:  ret
  }
  </code></pre>
  
  <p>Enum conversion has been treated extensively on SO, but I could not find a discussion addressing this specific case.</p>