7

Consider the following code:

class Base<T>
{
 //Base members
}

I want the generic T to be an enum (using constraints if possible). How can I do this in C#?

EDIT:
Using code contracts -introduced by Akash Kava- also seems like a nice way. I managed to get it to produce a run time error which is useless. Here's the code I tried. It should be possible to generate a compile time warning but I can't get it to work.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • Lots of them, but one of the canonical ones I can find is http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum, which although is about a generic method, not a generic class, type constraints work in exactly the same way anyway. – BoltClock Dec 31 '11 at 17:08
  • @BoltClock, Thanks for pointing out, but I still got a very different answer (I think a better one). However, I haven't still managed to get it to work. – atoMerz Dec 31 '11 at 17:29
  • Have you tried code contracts? – Akash Kava Dec 31 '11 at 19:31
  • @AkashKava: Never heard of 'em. Any links? – atoMerz Jan 01 '12 at 08:16
  • 1
    http://msdn.microsoft.com/en-us/devlabs/dd491992 , this is little alternative way of creating warnings.. – Akash Kava Jan 01 '12 at 12:10
  • @AkashKava, Alright I read into it a bit. If I get it right I should check T to be of type enum in a precondition? Right? – atoMerz Jan 01 '12 at 17:14
  • Yes, but I don't know how to do it exactly but looks like it is possible to generate some warning. – Akash Kava Jan 01 '12 at 17:34
  • @AkashKava, I managed to get it to check it at run time which is useless. I updated my post with the code I tried. – atoMerz Jan 02 '12 at 09:28
  • @AkashKava: I made it work. If you add an answer I'll vote it up. – atoMerz Jan 17 '12 at 10:08
  • possible duplicate of [Anyone know a good workaround for the lack of an enum generic constraint?](http://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint) – nawfal Jan 29 '14 at 10:45

2 Answers2

12

This is supported at the IL level but not in C#. You may take a look at unconstrained melody written by Jon Skeet which allows you to achieve that. And here's the corresponding blog post where he explains in details.

Bart
  • 5,065
  • 1
  • 35
  • 43
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Surprised there is no way in C#. :( – atoMerz Dec 31 '11 at 16:58
  • Out of curiosity, does the IL allow an enum-constrained generic to do anything an unconstrained generic can do other than invoke the slow `Enum.HasFlag`? My guess would be that the C# crew judged the level of IL support to be unsufficient for what people would want to do with enum-constrained generics, and thought (misguidedly IMHO) that it would be better to disallow them than to allow them but not have them be able to do much. – supercat Jan 12 '13 at 17:53
  • There's [ExtraConstraints.Fody](https://github.com/Fody/ExtraConstraints) to achieve all the IL-weaving in a very simple way. Just install the nuget packages `Fody` and `ExtraConstraints.Fody` and add the constraints to the code as shown on the repo. Maybe you would want to add this to the answer? – BatteryBackupUnit Jul 24 '17 at 06:52
1

.NET 4.0 onwards you can create warnings by using Code Contracts, http://msdn.microsoft.com/en-us/devlabs/dd491992, this will let you check some preconditions and display warnings to developer.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167