Skip to content

C# 9 covariant return types: Still can get System.TypeLoadException when using generics #45344

Description

@ASolomatin

Description

This looks like dotnet/core#5319 but with generics.

The C # 9.0 overridden virtual function works fine when the return type is a covariant derived type, continues to work if the return type is a covariant derived type that contains the overridden function. But System.TypeLoadException is thrown at runtime if you add some generics. As before, everything builds without errors and warnings.

Code to reproduce

using System;

var c = new AChild();

Console.WriteLine("Hello World!");

public abstract class A<T>
{
    public abstract B<T> GetB();
}

public class AChild : A<string>
{
    public override BChild GetB() => null;
}

public class B<T>
{
}

public class BChild : B<string>
{
}

Error:

$ dotnet run
Unhandled exception. System.TypeLoadException: Return type in method 'AChild.GetB()' on type 'AChild' from assembly 'N5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with base type method 'A`1[System.__Canon].GetB()'.
$ dotnet --version
5.0.100

Versions

5.0.100-rc.2.20479.15
5.0.100

Originally posted by @ASolomatin in dotnet/core#5319 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions