find and solve || findandsolve.com
Please wait.....

C# Interface

In the c#, the interface is a blueprint of a class which is like an abstract class because all methods are declared inside the interface are abstract methods.is cannot have a method body and cannot be instantiated.

The interface is used to achieve multiple inheritance which can't be achieved by class. It is used to achieve full abstraction because it cannot have a method body.

Its implementation must be provided by class or struct. The class or struct which implements the interface must provide the implementation of all the methods declared inside the interface.

Its accomplishment must be provided by class or struct. The class or struct which implements the interface must provide the accomplishment of all the methods declared inside the interface. 

C# interface example

Let's see the given example of the interface in C# which has the draw() method. Its accomplishment is provided by two classes: Tet and Test1.

using System;  

public interface Example
{
    void draw();
}
public class Test : Example
{
    public void draw()
    {
        Console.WriteLine("drawing rectangle...");
    }
}
public class Test1 : Example
{
    public void draw()
    {
        Console.WriteLine("drawing circle...");
    }
}
public class TestInterface
{
    public static void Main()
    {
        Example e;
        e = new Test();
        e.draw();
        e = new Test1();
        e.draw();
    }
}  

Output

drawing rectangle...

drawing circle...