Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the difference between declaring an abstract function within an interface and declaring a function in a non-abstract form?

I have a question while studying C#.
When I declare the interface, I usually declare it like the code below

public interface IRequireInitialization
{
    void Init();
}
public class TESTManager : IRequireInitialization
{
    public void Init(){ throw new NotImplementedException(); }
}

but my friend said that I should add abstract before the declaration of the interface member function.
I didn’t feel any big difference in implementing the interface, what’s the difference? And if there is a difference, when should I use it?

public interface IRequireInitialization
{
    abstract void Init();
}
public class TESTManager : IRequireInitialization
{
    public void Init(){ throw new NotImplementedException(); }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

C# 8 introduced default interface methods. This allows to define a default implementation (body) for a method in the interface itself.

As part of this proposal they relaxed the syntax to allow modifiers for interface methods:

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.

A method in an interface is abstract by default and abstract modified just makes that explicit:

similarly, although abstract is the default on interface members without bodies, that modifier may be given explicitly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading