简体   繁体   English

C# 8 预览中的抽象接口方法

[英]abstract interface methods in C# 8 preview

I try:我尝试:

public interface I { abstract void F(); }

I get:我得到:

The modifier 'abstract' is not valid for this item in C# 7.3.修饰符 'abstract' 在 C# 7.3 中对此项无效。 Please use language version 'preview' or greater.请使用语言版本“预览”或更高版本。

However I can find no mention of this feature ie in https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8但是我在https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8中找不到任何提及此功能的内容

Where can I find the docs for that ?我在哪里可以找到相关文档? or is the message wrong here ?还是这里的信息有误?

C# 8.0 will allow modifiers and default implementations for interface members. C# 8.0 将允许接口成员的修饰符和默认实现。 You can see discussion here and details here您可以在此处查看讨论,并在此处查看详细信息

However, the abstract modifier in an interface method makes zero sense IMO, but it might be available in the C# 8, since other modifiers will be valid as well.但是,接口方法中的abstract修饰符对 IMO 零意义,但它可能在 C# 8 中可用,因为其他修饰符也将有效。

You can see the abstract is listed in the allowed modifiers您可以看到abstract列在允许的修饰符

The syntax for an interface is relaxed to permit modifiers on its members.接口的语法被放宽以允许对其成员进行修饰符。 The following are permitted: private, protected, internal, public, virtual, abstract , sealed, static, extern, and partial.以下是允许的:private、protected、internal、public、virtual、 abstract 、sealed、static、extern 和 partial。

Like the other answer it is legal from c#8.0.与其他答案一样,它在 c#8.0 中是合法的。 But is there any benefit of having abstract methods in interface??但是在接口中使用抽象方法有什么好处吗?

With recent improvements in the language, abstract function in interface can be considered as something which will not have default implementation.随着语言的最新改进,接口中的abstract函数可以被认为是没有默认实现的东西。

Now the C# interfaces can have default implementation for interface functions.现在 C# 接口可以有接口函数的默认实现。 Look at the below example.看下面的例子。 It is totally fine and the output is "Default implemented example function".完全没问题,输出是“默认实现的示例函数”。

//AN INTERFACE WITH DEFUALT METHOD
interface I
{
  string Example()
  {
      return "Default implemented example function";
  }
}

//CLASS IMPLEMENTING THE INTERFACE
class C : I
{

}

//AN EXAMPLE CLASS ACCESSING THE DEFAULT IMPLEMENTED example FUNCTION.
public class Test
{
    public static void Main(string[] args)
    {
        I i = new C();
        Console.WriteLine (i.Example());
    }
}

In the above example lets suppose try adding abstract for the "Example()" function in Interface.在上面的示例中,我们假设尝试在接口中为“Example()”函数添加abstract

The code will not compile saying "'I.Example()' cannot declare a body because it is marked abstract"代码不会编译说“'I.Example()' 不能声明主体,因为它被标记为抽象”

So when we use abstract the obvious way is to define the function in the class (implements interface) for any usage(shown below).因此,当我们使用abstract时,显而易见的方法是在类(实现接口)中定义任何用途的函数(如下所示)。

//AN INTERFACE WITH ABSTRACT METHOD
interface I
{
  abstract string Example();
}

//CLASS IMPLEMENTING THE INTERFACE AND GIVE A BODY FOR EXAMPLE FUNCTION
class C : I
{
    public string Example()
    {
        return "Implemented example function";
    }

}

//A CLASS ACCESSING THE CLASS METHOD EXAMPLE.
public class Test
{
    public static void Main(string[] args)
    {
        I i = new C();
        Console.WriteLine (i.Example());
    }
}

In summary declaring the function in interface as abstract , there can not be a default body for the function in interface, instead the class implements the interface must have a body for it.总之,将接口中的函数声明为abstract ,接口中的函数不能有默认主体,而是实现接口的类必须有一个主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM