Cannot use interface default methods

夙愿已清 提交于 2019-12-02 02:36:29

问题


I have installed new version of visual studio and set the project to C# 8. When I tried to use new interface feature like below it said that I cannot implement in interface which seems that somehow I cannot use new feature. Can anyone explain why, is it still not included in 2019 preview?

interface IDefaultInterfaceMethod
{
    public void DefaultMethod()
    {
        Console.WriteLine("I am a default method in the interface!");
    }
}

回答1:


Based on this:

Microsoft has fleshed out more details about C# Version 8.0, the next planned major release of the language. C# 8.0 is expected to arrive in 2019, concurrent with the arrival of .Net Core 3.0.

All of the C# 8 features are still early in development and are likely to change. So it would be better to wait until the features end up in the final release. The following blog could be also helpful:

Any estimation for C# 8.0 release date?

Not all C# 8.0 features are available yet (Preview 1 of Visual Studio 2019). You can find the list of current available features in the following link:

Take C# 8.0 for a spin




回答2:


Default interface methods aren't available in .NET Core 3.0 Preview 1. This really is a preview preview - even async enumerables won't work without some manual fixes. The Roslyn team decided to release the preview even though some things wouldn't work right out :

Should we tell users to skip this feature until preview 2?

This is a preview, not RTM. If we told users to skip preview features because they had bugs we should just stop shipping previews altogether. 😄

Default interface implementations are available for experimentation through SharpLab.io. This site shows how the compiler works by displaying the C#, IL or assembly code the compiler generates for any example.

Sharplab.io allows using many different experimental compilers. The Default Interface Methods option can compile this example:

interface IDefaulter
{
    int GoBust()=>0;
}

Note the date though - that compiler hasn't been updated since April 2, 2018. The features released in Preview 1 were last update in October 2018.



来源:https://stackoverflow.com/questions/53694961/cannot-use-interface-default-methods

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