C# Interfaces Tutorial For Beginners With Examples

An interface contains signatures of methodspropertiesevents and indexers.
class or struct implementing the interface should provide implementation of the methods, properties, events and Indexers declared in the interface.

Creating Interfaces

Just like class, an interface is created using the interface keyword. Access modifiers cannot be used inside an interface. All the members of an interface are public by default.
    protected interface ICalculate
    {
        int multiply(int x, int y);
    }
        
An interface is very similar to an abstract class that has only abstract members.

Implementing Interfaces

A class or struct implementing an interface must provide functionalities of all the members declared in an interface.
    class Calculate: ICalculate
    {
        int result;

        public int multiply(int x, int y)
        {
            result = x * y;
            return result;
        }
    }
        

Implementing Multiple Interfaces

A class or struct can implement multiple interfaces.
Since, C# does not supports multiple inheritance so by using interfaces, behaviors of different entities can be implemented in a single entity. This serves the purpose of multiple inheritance.
    protected interface ICalculate
    {
        int multiply(int x, int y);
    }

    protected interface IEmployee
    {
        void DoWork(string work);
    }

    class Calculate: ICalculate, IEmployee
    {
        int result;

        public int multiply(int x, int y)
        {
            result = x * y;
            return result;
        }

        public void DoWork(string work)
        {
            //Provides implementation here.
        }
    }