How to restrict the generic type of enum type?

Consider the following code:

class Base<T> { //Base members } 

I want generic T to be an enumeration (using restrictions if possible). How to do it in C #?

EDIT:
Using the code contracts introduced by Akash Kava also seems nice. I managed to get him to create a runtime error that is useless. Here is the code I tried. It should be possible to create a compilation warning, but I can't get it to work.

+7
generics c # type-constraints
source share
2 answers

This is supported at the IL level, but not at C #. You can take a look at the unconditional melody written by Jon Skeet, which allows you to achieve this. And here is the corresponding blog post , where he explains in detail.

+12
source share

.NET 4.0 onwards you can generate alerts using Code Contracts, http://msdn.microsoft.com/en-us/devlabs/dd491992 , this will allow you to check some prerequisites and display warnings for the developer.

+1
source share

All Articles