Generic return values and explicit casting
Came across an artifact with generics that confused me. Let's say you had an interface IFoo
:
and a class Foo
that implements IFoo
:
then a given method returning IFoo
doesn't have to explicitly cast Foo
to the interface:
So, I figured that the same would be true for a generic method returning T
with a constraint to IFoo
. However, it seems that the constraint is not considered for implicit casting to T
. So you have to explicitly cast Foo
to IFoo
and then cast that to T
:
That just seems a bit strange to have to do.