Covariant method argument

Alexander 1 Reputation point

Some languages (e.g., Eiffel), accept covariant method argument redefinition and employ some other mechanisms to address type safety issues that arise in this case. When I tried to mimic this 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 covariant method argument type (I used a generic parameter with a covariant type mark) 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?

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: The period of time during which a program is being executed in a computer.
1,043 questions
{count} votes

1 answer

Sort by: Most helpful