C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used to achieve full abstraction because it can't have normal methods. In C#, multiple inheritance is achieved by interface. Interface can't be instantiated. If a class is implementing an interface then it must give definition to all of its methods. Let’s see an example of interface in C#.

using System;
public interface Drawable
{
            void show();
}
public class Circle:Drawable
{
            public void show()
            {
                        Console.WriteLine("drawing Circle....");
            }
}
public class InterfaceExample
{
            public static void Main (string[] args)
            {
                        Drawable d;
                        d=new Circle();
                        d.show();
            }
}
Output
drawing Circle....



ADVERTISEMENT
ADVERTISEMENT