In this lesson, you will learn about interface in C# with example. Also, you will learn the difference between abstract class and interface.

Definition of Interface

Interfaces are objects in C# that looks as same as a class, but with no implementations. In other words, the interface contains a declaration or signature of methods, events, properties, or indexer. So, you are wondering, what is the use of declaration without implementation? Well, every declaration in an interface will have its associated implementations in a separate class.   

The beauty of the interface is that it allows the developer to put the declaration only in one place which will make it easier to architect and understand what the implementing class contains. The class implementing the interface should contain the exact members in the interface or it will throw an error. A good point to remember that interface member can only be public, and there are public by default. 

Syntax of Interface in C#

    interface MyIntefrace
    {

        // interface declarations 

    }

Note: It’s a good practice to start the interface name by the capital letter ‘I’ to easy distinguish it from other classes and to stay consistence with the. NET frameworks built-in interfaces.

Example I of interface in C# 

    interface IProgram
    {
        void WriteMyName();
    }

 class Program: IProgram
    {

        public void WriteMyName()

        {

            Console.Write("TutorialsPanel.com");

        }
    }


    static void Main(string[] args)
    {
        Program program = new Program();

        program.WriteMyName();

        Console.ReadKey();
    }

Output

Example II of interface in C#

    interface ICar
    {
        string Model

        {
            get; set;

        }

        string Color

        {
            get; set;
        }

        void TurnEngineOn();

        void DisplayInfo();
    }


 class Program : ICar
    {
        public string Color

        {
            get;
            set;
        }

        public string Model

        {
            get;
            set;
        }

        public void TurnEngineOn()

        {
            Console.WriteLine("The car is on");
        }

        public void DisplayInfo()

        {
            Console.WriteLine("My car is a " + Color + " " + Model);

        }

    }

    static void Main(string[] args)
    {
      Program _car = new Program();

        _car.Color = "red";

        _car.Model = "BMW";

        _car.TurnEngineOn();

        _car.DisplayInfo();

        Console.ReadKey();
    }

Difference between interface and abstract class

Abstract ClassInterface
Abstract class contains Data Member.Interface Does not contain Data Member.
Abstract Class Does not support multiple inheritanceInterface supports multiple inheritance.
Abstract class contains both abstract and complete members. Interface contains only the signature of the member.
Abstract class can have access modifier for all members.Interface cannot have an access modifier.
Abstract class can have static member.Interface can not have a static member.
Abstract class can have constructors.Interface can not have constructors.

 

Last modified: January 5, 2019

Comments

Write a Reply or Comment

Your email address will not be published.