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. I learnt the following from my investigation:

Following is an excerpt from the C# language specification for generic constraints

A _class-type_ constraint must satisfy the following rules:

- The type must be a class type.
- The type must not be sealed.
- The type must not be one of the following types: `System.Array`, `System.Delegate`, `System.Enum`, or `System.ValueType`.

- The type must not be `object`. Because all types derive from `object`, such a constraint would have no effect if it were permitted.

- At most one constraint for a given type parameter can be a class type.

A type specified as an _interface-type_ constraint must satisfy the following rules:

- The type must be an interface type.
- A type must not be specified more than once in a given `where` clause.

There you have it. The specification deliberately restricts value types and enums as generic type parameters. But if you wish to specify a non-reference type as the primary constraint, a struct can be used.

private void Method where T : struct

We know that the numeric types like int, float etc in C# are declared struct (value type). An int (Int32) is declared as follows:

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

Whereas an enum, though a value type, is declared as an abstract class that derives from System.ValueType; unlike int or float. However, the end result is enums are value types. Go figure!

Anyway, the question still remains unresolved – why enums cannot be used as constraints. Because the language specification says so is not satisfactory.

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


P.S. Refer section 25.7 through for the specification on Generic Type Constraints.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s