serhio
serhio

Reputation: 28586

Generics and Enumerations

How to use(constraint) an Enumeration as a generic parameter in .NET?

I used somthing like

Public Function GetEnumStringValues(Of EType As {Structure, _ 
                   IComparable, IConvertible, IFormattable})() As List(Of String)

but this is not good.

Upvotes: 1

Views: 112

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156748

You can use enum types as generic parameters to a method (for example List<EType>, but you cannot restrict generic parameters to only be enum types.

However, there are tricks you can use to almost guarantee that only enums get used in your methods:

public static T ParseEnum<T>(this string enumValue)
    where T : struct, IConvertible

See Converting string back to enum for a more full explanation and code samples.

Upvotes: 3

Aliostad
Aliostad

Reputation: 81700

Not possible I am afraid.

It has been requested though.

Jon Skeet has a workaround for it.

Upvotes: 2

Related Questions