Ads

C# Programming 8 - Can we add a method with implementation in Interface?

Hello Friends,

Can we add a method with implementation in Interface? 

What is this question, I think question is wrong, is it?

No....... Its correct

We can achieve this by Default Interface Methods Introduced In C# 8.0


The default interface method is the virtual extension method and it is a new feature introduced in C# 8.0

Default Interface Methods allows us to add a method with implementation without breaking the existing functionality in Interface. Before C# 8.0 an interface was allowed to contain only (signature) the declaration part of methods, but now after  interface can contain both the declaration and the implementation.

Virtual extension methods enable an API author to add methods to an interface in future versions without breaking source or binary compatibility with existing implementations of that interface.

Below Is the simple console application helps us to understand the above detail in practically.

1. Create Console Application


2. Create Interface called IILogInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DefaultInterface
{
    public interface IILogInfo
    {
        public string GetMyName();
        public string GetMessage()
        {
           return "Hello I am Virtual Extension Methos Introduced in C# 8.0";
        }
    }
}

Note, that above interface has method with complete implementation i.e. GetMessage()

3. Create Implementation for IILogInfo i.e. LogInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DefaultInterface
{
    public class LogInfo : IILogInfo
    {
        public string GetMyName()
        {
            return "I am General Method Followed By Interface Existing";
        }
    }
}

4. Now Program.cs we are calling both method

using DefaultInterface;

public class Program
{
    private readonly IILogInfo logInfo;
    public Program(IILogInfo logInfo)
    {
        this.logInfo = logInfo;
    }

    public static void Main(string[] arg)
    {
        Program pgm=new Program(new LogInfo());
        Console.WriteLine(pgm.logInfo.GetMyName());
        Console.WriteLine(pgm.logInfo.GetMessage());
        Console.ReadLine();
    }
}

5. Finally Output is


Conclusion:

It helps us to add extra method to existing interface and it will not break any clients. 

Our application contains some interface IProduct, and now we have decided that we need an extra method GetProductByLocation() on it. 

The main problem is:

We cannot add another method GetProductByLocation() to IProduct, because that would break existing client implementing IProduct (because they don't implement GetProductByLocation()), and We can't change IProduct to an abstract base class, because that, as well, would break existing classes implementing IProduct, and we will lose the ability to do multiple inheritance.

Finally Solution is Default Interface Method.

That's it for this article.....

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !