问题
Why C#8 Default implementations of interface members will report an error?
public interface Logger
{
void Info(string message);
void Error(string message);
// C#8 Default implementations of interface
void Warn(string message)
{
// "interface method cannot declare a body" error message
}
}
and
.NET Core 3.0 is configured as shown in the screenshot.
回答1:
This is a Resharper/Rider bug: https://youtrack.jetbrains.com/issue/RSRP-474628
回答2:
The feature is sound and your setup is correct. Also the following works for me, i can compile and run the following in .Net Core 3
class Program
{
interface IDefaultInterfaceMethod
{
void DefaultMethod()
{
Console.WriteLine("I am a default method in the interface!");
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Note though, i get your error in the IDE!! yet there was no error in the error window. However, it still compiles and runs so it is not C#8.. Seeing this is a sure-sign something else is the issue
In Short, this is likely a Resharper problem, when i suspended Resharper the false positive went away
来源:https://stackoverflow.com/questions/58110983/why-c8-default-implementations-of-interface-members-will-report-an-error