Reputation: 13058
Let's say I have a class structure like this, where class Foo
is a generic over a class hierarchy of Base
, Derived1
, etc.
abstract class Base {...}
class Derived1 : Base {...}
.
.
.
class Derivedn : Base {...}
class Foo<T> where T : Base
{
/// XXX is a placeholder - see below
void DoBar(XXX arg) {...}
...
}
Wanting DoBar()
to operate on something derived from Base
, does it make any difference if XXX
is Base
or is T
?
The constraint means that T
has to be Base
or one of its children; but within the scope of DoBar()
it is going to treat it as an object of type Base
either way -- so this seems superficially like it will make no difference. But I might be overlooking something more subtle.
Upvotes: 0
Views: 40
Reputation: 1679
If you set xxx
to Base
than your method parameter can be Base
or any class derived from Base
.
If you set xxx
to T
than your method parameter can be T
class or any class derived from T
.
So if you instantiate Foo
class with the generic type of Derived1
for example, in the first case you can use Base
or classes derived from Base
and in the second case you can use only Derived1
or classes derived from Derived1
Upvotes: 4