Linked Questions

31 votes
4 answers
19k 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 [...
Alex Essilfie's user avatar
17 votes
3 answers
16k 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 class&...
Simon Dugré's user avatar
  • 19.1k
5 votes
2 answers
10k 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 ...
PositiveGuy's user avatar
  • 47.9k
5 votes
2 answers
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 ...
Carlo V. Dango's user avatar
2 votes
1 answer
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 () ...
Raheel Khan's user avatar
  • 14.8k
-4 votes
1 answer
853 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 ...
NoWar's user avatar
  • 37.8k
5 votes
1 answer
185 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 ...
Anders Gustafsson's user avatar
2 votes
0 answers
232 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 ...
Class Skeleton's user avatar
1 vote
0 answers
103 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 ...
sensei's user avatar
  • 7,702
0 votes
0 answers
70 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 ...
MVAmorim's user avatar
  • 105
0 votes
0 answers
56 views

C# passing any enum to a class constructor [duplicate]

The following is a wrapper class that will be passed an instance of HEventProperty in order to sync it with HDropdown. In this case the T in HEventProperty will always be an enum, but we don't know ...
arcadeperfect's user avatar
0 votes
1 answer
42 views

How to write a generic function for enum types [duplicate]

//TechTeam is enum type //This line returns the enumvalue as out param, for the matching stringvalue. Enum.TryParse(stringvalue, out TechTeam tt); It works perfectly. Now if I turn it to generic I ...
Venkat's user avatar
  • 2,256
0 votes
0 answers
24 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, } ...
ditoslav's user avatar
  • 4,918
203 votes
11 answers
137k views

Getting the max value of an enum

How do you get the max value of an enum?
jdelator's user avatar
  • 4,291
115 votes
14 answers
60k 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) ...
dotnetnoob's user avatar
  • 11.5k

15 30 50 per page
1
2 3 4 5
7