Linked Questions
22 questions linked to/from Enum type constraints in C#
97 votes
12 answers
24k views
Anyone know a good workaround for the lack of an enum generic constraint?
What I want to do is something like this: I have enums with combined flagged values. public static class EnumExtension { public static bool IsSet<T>( this T input, T matchTo ) ...
38 votes
2 answers
56k views
Using enum as generic type parameter in C# [duplicate]
Possible Duplicate: Enum type constraints in C# Is it possible to use enum types as a generic paramter by using its wrapper class Enum? I have different enums: enum errors1 { E1, E3, E8 }; enum ...
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 () ...
1 vote
1 answer
1k views
C# Generics: Enum constraints are not allowed, Why? [duplicate]
Possible Duplicate: Enum type constraints in C# Could somebody kindly explain to me with a simple sample and simple words why enum constraints are not allowed on a generic type i.e. What would ...
972 votes
319 answers
504k views
Strangest language feature
What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered? Please only one feature per answer.
141 votes
10 answers
51k views
Cast Int to Generic Enum in C#
Similar to Cast int to enum in C# but my enum is a Generic Type parameter. What is the best way to handle this? Example: private T ConvertEnum<T>(int i) where T : struct, IConvertible { ...
49 votes
3 answers
34k views
Can I cast from a generic type to an enum in C#?
I'm writing an utility function that gets a integer from the database and returns a typed enum to the application. Here is what I tried to do (note I pass in a data reader and column name instead of ...
23 votes
8 answers
24k views
Is there a C# generic constraint for "real number" types? [duplicate]
Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up a Cartesian coordinate system in C#, but I don't want to restrict myself to any one numerical type for ...
29 votes
2 answers
5k views
Why there is no something like IMonad<T> in upcoming .NET 4.0
... with all those new (and not so new if we count IEnumerable) monad-related stuff? interface IMonad<T> { SelectMany/Bind(); Return/Unit(); } That would allow to write functions that ...
19 votes
5 answers
17k views
Behaviour to simulate an enum implementing an interface
Say I have an enum something like: enum OrderStatus { AwaitingAuthorization, InProduction, AwaitingDespatch } I've also created an extension method on my enum to tidy up the displayed ...
11 votes
4 answers
4k views
Is there a workaround for generic type constraint of "special class" Enum in C# 3.0? [duplicate]
Update: See the bottom of this question for a C# workaround. Hi there, Consider the following extension method: public static bool HasFlags<T>(this T value, T flags) where T : System.Enum { ...
9 votes
4 answers
623 views
Is there a way to set an Enumerated Property in a class to All available enums?
First things off, I had no idea what to title this question - I'm even confused how to state it. Now for the question. Let's take the System.IO.FileSystemWatcher class where you set it's ...
8 votes
1 answer
7k views
How do I convert an enum between (string, list of flags, enum with flags)?
How do I convert from enums to strings and vice versa? And taking the case that enums can contain multiple flags, how can I get a list of all the flags an enum contains?
4 votes
2 answers
10k views
How to declare an Enum to be used by a generic type?
I would like to declare in a parent abstract class something along the lines of: public abstract void RefreshDisplay<TView>(Enum value); Which would then be implemented in the child class ...
3 votes
2 answers
7k views
Generic Nullable Enum [duplicate]
I would like to have a method that will parse from a nullable database column an enum. I wrote this method below (and had to restrict T to a struct to make it compile). It does compile, but I believe ...