Can we make a enum as a generic data type? If so please provide an example. Thanks in advance.
Asked
Active
Viewed 456 times
2
-
2What exactly do you want to use it for? – Oded Jan 28 '10 at 09:47
-
We are having many enums in our project which come under various category like the types of domestic meters, commercial meters etc, now instead of mapping the usual integar value to the name we were thinking of mapping the meter code with it. Hence I thought if the enums could be generic we could make classes of meter types and create generic enums. I hope it clear my question. – HotTester Jan 28 '10 at 09:56
4 Answers
7
-
The example given is great and maps what i was looking for. Thanks ! – HotTester Jan 29 '10 at 04:40
2
No. Enums in C# have an integral base type (Int32 by default) but the items in it are always known at compile-time. There is simply no sensible way how generics might even fit into the concept of an enum.

Joey
- 344,408
- 85
- 689
- 683
1
You can try class enums.

Arnis Lapsa
- 45,880
- 29
- 115
- 195
-
Thanks for the link. It really is the way i wanted to solution to be ... but to be frank its a lot complicated ! Can u share a less complicated example ? – HotTester Jan 28 '10 at 11:05
-
0
You can pass an enum
as a type parameter to a generic method whose constraints allow it, but the enum
itself cannot be generic.

Jay
- 56,361
- 10
- 99
- 123