Covariant method argument

Alexander 1 Reputation point
2022-09-09T13:50:11.933+00:00

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: An environment required to run apps that aren't compiled to machine language.
1,160 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,311 Reputation points
    2022-09-10T15:17:35.613+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.