Generic Type Parameter constraints in C# .NET

Posted by activwerx on Stack Overflow See other posts from Stack Overflow or by activwerx
Published on 2012-11-12T13:24:30Z Indexed on 2012/11/13 11:01 UTC
Read the original article Hit count: 249

Filed under:
|
|
|

Consider the following Generic class:

public class Custom<T> where T : string
{
}

This produces the following error:

'string' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter.

Is there another way to constrain which types my generic class can use?

Also, can I constrain to multiple types?

E.G.

T can only be string, int or byte

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET