Why C#8 Default implementations of interface members will report an error

前提是你 提交于 2020-01-15 12:36:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!