I have a question in .NET Generics regarding the constraints.
How would I prevent a value type from being used as the type parameter?
say i have a generic class Foo
class Foo<T> {
}
I want to prevent T from being a value type say only structs. Integral value types are ok. (int,float,doub le). what i want is the effect something like
class Foo<T> where T not struct //instead of the T : struct
{
T()
{
//if type of T is a struct type throw exception.
}
}
I know there isnt a direct way, but i am looking at something like a check at ctor and throw an exception if the type is determined to be a struct type.
How would I prevent a value type from being used as the type parameter?
say i have a generic class Foo
class Foo<T> {
}
I want to prevent T from being a value type say only structs. Integral value types are ok. (int,float,doub le). what i want is the effect something like
class Foo<T> where T not struct //instead of the T : struct
{
T()
{
//if type of T is a struct type throw exception.
}
}
I know there isnt a direct way, but i am looking at something like a check at ctor and throw an exception if the type is determined to be a struct type.
Comment