Covariant method arguments

New Contributor

Some languages (such as Eiffel), accept covariant method argument redefinition and employ some other mechanisms to address type safety issues that arise in this case. When I try to use method arguments of a generic parameter type marked as covariant in C#, I get a compile-time error. Doing the same in plain IL successfully passes compilation, but causes a run-time error. In both cases (compile-time error for C# and run-time error for IL), the message says that the method argument type cannot be covariant for type safety reasons.

 

When generics were introduced, the design paper claimed that covariant generics are supported to enable different programming languages, although in non-verifiable .NET code. Is it still the case, or was the idea of supporting languages with non-C# type systems abandoned? In particular, is there any mechanism (e.g., some attributes) to prevent .NET run-time from verification of arguments with covariant types until execution?

0 Replies