where enum does not work !!!

I was writing a generic method with enum as the Constraint, and the compiler spat a few errors that did not directly convey me that enums cannot used as generic constraints. And I learnt the following from my investigation:

C# language specification for generic constraints

A class-type constraint shall satisfy the following rules:

  • The type shall be a class type.
  • The type shall not be sealed.
  • The type shall not be one of the following: types: System.Array, System.Delegate, System.Enum or System.ValueType.
  • The type shall not the <strong>object</strong>. [Note: Since all types, derive from <strong>object</strong> such a constraint would have no effect if it were permitted, end note]
  • At most one constraint for a given type parameter can be a class type.

A type specified as an interface-type constraint shall satisfy the following rules:

  • The type shall be an interface type.

This is an excerpt from the C# Language Specification. Enums are value types and there is no way that you can specify the System.ValueType as a constraint, as per the specification. But if you wish to specify a non-reference type as a [primary] constraint, struct can be used.

private void Method where T : struct

During the course of investigation, I was surprised (my bad!) to know that the numeric types like int, float etc in C# are declared struct. It is not far from the fact that they are value types, but it was interesting to know that they are declared as:

public struct Int32 : IComparable, IFormattable, IConvertible, IComparable, IEquatable

Similar thing for other numeric types. Whereas an enum [System.Enum], though a value type, is declared as an abstract class that derives from System.ValueTypes unlike the int or float. The end result is that enums are value types but i wonder the way they are declared.

Anyway, the question still remains unresolved – why enums cannot be used as constraints, and just the specification saying that enums cannot be used as constraints does not seem satisfactory.

I am not sure if there is any other way to resolve my situation. Question open to cyber space !!!

<blockquote>
  <p>
    P.S. Refer section 25.7 through for the specification on Generic Type Constraints.
  </p>
</blockquote>