Reputation: 10236
My code is as follows
class BaseClass<T> where T : class
{
class DerivedClass<U, V>
where U : class
where V : U
{
BaseClass<V> _base;
}
}
error: The type 'V' must be reference type.
Isn't 'V' here of type class ??
Upvotes: 0
Views: 100
Reputation: 73462
Isn't 'V' here of type class ??
No it is not. V
could be of System.ValueType
or any enum, or any ValueType
.
Your constraint just says that V
should be derived from U
where as U
is class. It doesn't says that V
should be a class.
For example the following is perfectly valid, which contradicts with the constraint where T : class
.
DerivedClass<object, DateTimeKind> derived;
So you need to add where V : class
also.
Eric Lippert has blogged the very same question.
Upvotes: 3
Reputation: 54117
You can resolve this issue by adding a class
constraint to the V
type parameter:
class BaseClass<T> where T : class
{
class DerivedClass<U, V>
where U : class
where V : class, U
{
BaseClass<V> _base;
}
}
For an explanation, see from Eric Lippert's article (as commented above by Willem van Rumpt).
Upvotes: 6