Certainly the given code could be compiled and run safely:
When a Something
instance is typed as Something
or as BaseClass
the compiler would allow any type for T
, while when the same instance is typed as ISomething
it would allow just types inheriting Foo
. In both cases you get static checking and runtime safety as usual.
In fact, the above scenario is exactly what happens when you implement ISomething
explicitly. So let's see what arguments we can make for and against the current state of affairs.
For:
- the proposed solution would not be applicable to all cases; it depends on the exact method signatures (is the type argument covariant? contravariant? invariant?)
- it doesn't require the specification to be amended with new text stating how such cases are handled
- it makes the code self-documenting -- you don't have to learn said text; the current rules regarding explicit interface implementation are enough
- it does not impose development costs on the C# compiler team (documentation, feature implementation, testing, etc)
Against:
- you need to type more
Considering the above and additionally the fact that this is not an everyday scenario, IMHO the conclusion to be reached is clear: this might be nice to have, but it certainly doesn't warrant going out of your way to implement it.