0

I try:

public interface I { abstract void F(); }

I get:

The modifier 'abstract' is not valid for this item in C# 7.3. Please use language version 'preview' or greater.

However I can find no mention of this feature ie in https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

Where can I find the docs for that ? or is the message wrong here ?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
kofifus
  • 17,260
  • 17
  • 99
  • 173

2 Answers2

3

C# 8.0 will allow modifiers and default implementations for interface members. 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.

You can see the abstract is listed in the allowed modifiers

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 3
    The use _might_ be to to re-abstract a method further down an interface hierarchy after a default implementation was added higher up to force implementing classes to implement the method as well, instead of taking the default. – Jonathon Chase Apr 29 '19 at 23:31
  • so what will happen if I upgrade to c# 8 preview - it will just compile and do nothing ? – kofifus Apr 29 '19 at 23:33
  • 1
    yes, in your example `abstract` should make no difference. – Selman Genç Apr 29 '19 at 23:34
0

Like the other answer it is legal from 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.

Now the C# interfaces can have default implementation for interface functions. 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.

The code will not compile saying "'I.Example()' cannot declare a body because it is marked abstract"

So when we use abstract the obvious way is to define the function in the class (implements interface) for any usage(shown below).

//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.

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34