views:

162

answers:

1

Hi folks,

i'm trying to do the following extension method -> converting an int to a enum, when you provide the enum :-

public static T ToEnum<T>(this int value)
{
    return (T)Enum.ToObject(typeof(T), value);
}

Now, i was hoping to make it so that you can only define the type T to be an enumeration. Is there any what i can restrict it?

eg.

int day = 3;
DaysOfWeek dow = day<DaysOfWeek>(); // No compiler error.
DaysOfWeek dow2 = day<Foo>(); // Compiler error.
+1  A: 

Use Where T: struct

See this Question http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum

TT
cheers! what a quick answer :)
Pure.Krome