Linked Questions
91 questions linked to/from Create Generic method constraining T to an Enum
28
votes
4answers
17k views
How to limit a generic type parameter to System.Enum [duplicate]
Possible Duplicates:
Anyone know a good workaround for the lack of an enum generic constraint?
Create Generic method constraining T to an Enum
Is is possible to limit the generic type parameter ...
15
votes
3answers
15k views
“Class of <T> where T : Enum” not working [duplicate]
Possible Duplicate:
Create Generic method constraining T to an Enum
Is there any reason why we can't do this in C#? And, if possible, how can I do something similar!
What I want :
public ...
3
votes
2answers
6k views
Method to take in any Enum [duplicate]
Possible Duplicate:
Create Generic method constraining T to an Enum
is it possible to create a generic method that takes in any enum? I'll then check the incoming type to first make sure it's an ...
5
votes
2answers
1k views
Generic C# method taking where the enum value as a parameter [duplicate]
Possible Duplicate:
Create Generic method constraining T to an Enum
Given a generic method that only operates on enum values
static void <T> method(T enum) where T ?????
{
// do ...
2
votes
1answer
4k views
System.Enum as a generic type parameter with constraints [duplicate]
Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#
Consider the following class:
public class Transition<TState>
{
public Transition ()
...
-4
votes
1answer
655 views
Cannot define T in C# [duplicate]
Any clue how to define T in this code?
public static T ToEnum<T>(this string value, T defaultValue)
{
if (string.IsNullOrEmpty(value))
{
return ...
5
votes
1answer
149 views
Possible to constrain generic type to enums? [duplicate]
I have a generic class Foo<T> where I want to constrain T to be an enum type. Is this possible in C#?
I have tried
public class Foo<T> where T : enum // COMPILATION ERROR
but this ...
2
votes
0answers
158 views
Enum flags wrapper in C# [duplicate]
I am refactoring some code for several enum collections that support the [Flags] attribute.
I am trying to come up with a generic class that allows for common methods (adding, removing and checking ...
1
vote
0answers
85 views
How to pass generic enum parameter to method? [duplicate]
public static string GetCustomerIssueStatus(this HtmlHelper html, byte number)
{
return Enum.GetName(typeof(Shared.Enumerators.CustomerIssueStatus), number);
}
How could I create this extension ...
0
votes
0answers
29 views
Generic Enum param in abstract method [duplicate]
I'm trying to make an abstract Object Factory and I tryied a few things and none of them worked.
What I would like to have is something like this that could be overrided:
public abstract ...
0
votes
0answers
23 views
How to have a generic function which uses any flag enum? [duplicate]
So I wan't to have a generic function which works on any enum which is a flag and do a bitwise operation on it.
something like:
[Flags]
public enum MyEnum: long
{
A = 0,
B = 1 << 0,
}
...
696
votes
36answers
381k views
How do you create a dropdownlist from an enum in ASP.NET MVC?
I'm trying to use the Html.DropDownList extension method but can't figure out how to use it with an enumeration.
Let's say I have an enumeration like this:
public enum ItemTypes
{
Movie = 1,
...
158
votes
11answers
84k views
60
votes
13answers
32k views
How do I cast a generic enum to int?
I have a small method that looks like this:
public void SetOptions<T>() where T : Enum
{
int i = 0;
foreach (T obj in Enum.GetValues(typeof(T)))
{
if (i == 0)
...
29
votes
7answers
18k views
Sort a List<T> by enum where enum is out of order
I have a List of messages.
Each message has a type.
public enum MessageType
{
Foo = 0,
Bar = 1,
Boo = 2,
Doo = 3
}
The enum names are arbitrary and cannot be changed.
I need to ...