Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS compiler accepts contradicting signature and fails to accept exact signature when overriding a generic abstract method #76914

Open
keenrain9910 opened this issue Jan 24, 2025 · 0 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@keenrain9910
Copy link

keenrain9910 commented Jan 24, 2025

This compiles:

#nullable enable

using System;
					
namespace Demo
{
    public interface ITest
    {
        public bool TryGet<T>(out T? result) where T : notnull;
    }

    public abstract class MyAbstractClass : ITest
    {
        public abstract bool TryGet<T>(out T? result) where T : notnull;
    }

    public class MyDerivedClass : MyAbstractClass
    {
        public override bool TryGet<T>(out T result)
        {
            throw new Exception("");
        }
    }
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

This fails:

#nullable enable

using System;
					
namespace Demo
{
    public interface ITest
    {
        public bool TryGet<T>(out T? result) where T : notnull;
    }

    public abstract class MyAbstractClass : ITest
    {
        public abstract bool TryGet<T>(out T? result) where T : notnull;
    }

    public class MyDerivedClass : MyAbstractClass
    {
        public override bool TryGet<T>(out T? result)
        {
            throw new Exception("");
        }
    }
}

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

public override bool TryGet<T>(out T result) signature, the one the compiler is happy to accept, contradicts both inherited definitions while it rejects public override bool TryGet<T>(out T? result) which is exactly the same as in parents.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant