dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.05k stars 4.03k forks source link

classes inheriting from an interface with default implementation can't access it without explicit casting #40788

Closed bigworld12 closed 4 years ago

bigworld12 commented 4 years ago

Version Used: 3.1.100 sdk , .net standard 2.1 project

Steps to Reproduce:

interface IA
{
    string MyString => "hey";
}
class AImp : IA
{
    public string OtherString => MyString + " dotnet";
}

Expected Behavior: should work fine

Actual Behavior: getting an error "the name MyString doesn't exist in the current context"

Work Around: explicit casting

interface IA
{
    string MyString => "hey";
}
class AImp : IA
{
    public string OtherString => ((IA)this).MyString + " dotnet";
}

cf : https://github.com/dotnet/roslyn/issues/17952

gafter commented 4 years ago

It has long been the case that methods in an interface are not inherited into classes that implement that interface. The default implementation for the method doesn't change that. The compiler is behaving as designed.