Interface in C#

C# does not support multiple inheritance. You can use interface to achieve multiple inheritance. An interface is like an abstract class, but you cannot provide implementation in it. You can only write signatures of methods, properties, events or indexers in interface. A class that implements an interface, must be implement all members of that interface.

Let us understand the interface with example.

1. Interfaces cannot contain fields

using System;

namespace ConsoleApplication1
{
    interface IMyInterface
    {
        int intVar;
    }
    class MyClass: IMyInterface
    {
        public void show()
        {
            Console.WriteLine("welcome at TutorialRide");
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.show();
            Console.ReadLine();
        }
    }
}


In the above interface, only an integer variable intVar is declared. When you compile this program, it will give error. You cannot declare variable in interface.

2. In interface, public modifier cannot be used and cannot contain implementation of methods.

using System;

namespace ConsoleApplication1
{   
    interface IMyInterface
    {        
        public void myFunction()
        {
            Console.WriteLine("Interface Demo");
        }
    }
    class MyClass: IMyInterface
    {
        public void show()
        {
            Console.WriteLine("welcome at TutorialRide");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.show();
            Console.ReadLine();
        }
    }
}


In the above interface, a public method is implemented. It will give error that
Error: The modifier 'public' is not valid for this item
When you remove the public modifier, it will give the following error.
Error: interface members cannot have a definition
'ConsoleApplication1.MyClass' does not implement interface member.

So always remember that interface members cannot have a definition.  

3. The implemented methods of interface in the class, must have public modifier.

using System;

namespace ConsoleApplication1
{   
    interface IMyInterface
    {
         void myFunction();       
    }
    class MyClass: IMyInterface
    {       
         void myFunction()
        {
            Console.WriteLine("welcome at TutorialRide");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
           // obj.myFunction();
            Console.ReadLine();
        }
    }
}


myFunction() method in interface is by default public. If you forget to append the public modifier with method myFunction() in class MyClass, then it will give compile time error. Because myFunction() is by  default public and if you not write public with this method in class, then it will be consider as private. You cannot use as a method as a private if it is already public.

So it is not enough to implement method in class that’s signature is in interface but it also should be declared as public.

4. A class can implement more than one interface but inherit only one class.

using System;

namespace ConsoleApplication1
{
    interface FirstInterface
    {
         void myFirstFunction();
    }
    interface SecondInterface
    {
        void mySecondFunction();
    }
    class MyClass : FirstInterface, SecondInterface
    {
        public void myFirstFunction()
        {
            Console.WriteLine("myFirstFunction() is called");
        }
        public void mySecondFunction()
        {
            Console.WriteLine("mySecondFunction() is called");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.myFirstFunction();
            obj.mySecondFunction();
            Console.ReadLine();
        }
    }
}


5. Different interface can contain same methods (name and signature of method)

using System;

namespace ConsoleApplication1
{   
    interface FirstInterface
    {
         void myFunction();       
    }
    interface SecondInterface
    {
        void myFunction();
    }
    class MyClass : FirstInterface, SecondInterface
    {
         void FirstInterface.myFunction()
        {
            Console.WriteLine("myFunction() is from FirstInterface");
        }
         void SecondInterface.myFunction()
        {
            Console.WriteLine("myFunction() is from SecondInterface");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            FirstInterface FI = new MyClass();
            FI.myFunction();
            SecondInterface SI = new MyClass();
            SI.myFunction();
            Console.ReadLine();
        }
    }
}


myFunction() method is available in both the interface. It has same signature in both interfaces. You can use both of the methods by using fully qualified name or explicitly implemented the interface. Please keep in mind, in this case the method implemented in the class are private. You cannot use public modifier (Explicit interface implimentation).

Properties in Interface

Properties in interface are look like the auto implemented properties but there are some differences.
  • The interface declaration of property only specifies the name and type of the property not implementation.
  • You cannot use modifiers, when a property is declared in an interface.

using System;
namespace ConsoleApplication1
{
    interface IEmployee
    {
        int empID
        {
            get;
            set;
        }
        string empName
        {
            get;
            set;
        }        
    }
    class Employee:IEmployee
    {
        int id;
        string name;
        public int empID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public string empName
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            System.Console.Write("Enter employee ID: ");
            emp.empID = int.Parse(System.Console.ReadLine());        
            Console.Write("Enter the name of the employee: ");
            emp.empName = System.Console.ReadLine();
            System.Console.WriteLine("The employee information:");
            System.Console.WriteLine("Employee ID: {0}", emp.empID);
            System.Console.WriteLine("Employee name: {0}", emp.empName);
            Console.ReadLine();
        }
    }
}

Interfaces Can Be Inherited

One interface can inherit another interface. The syntax is the same as for inheriting classes. It is responsibility of a class that implements a second interface that, it must provide implementations for all the members defined within the interface hierarchy.

Here is an example.

Example

using System;

namespace ConsoleApplication1
{
    interface FirstInterface
    {
        void myFirstFunction();
    }
    interface SecondInterface:FirstInterface
    {
        void mySecondFunction();
    }
    class MyClass :SecondInterface
    {
       public void myFirstFunction()
        {
            Console.WriteLine("myFirstFunction() is from FirstInterface");
        }
       public void mySecondFunction()
        {
            Console.WriteLine("mySecondFunction() is from SecondInterface");
        }
    }    
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.myFirstFunction();
            obj.mySecondFunction();
            Console.ReadLine();
        }
    }
}


Key points about the interface.

  • An interface is like an abstract base class but in interface only signature of members are allowed.
  • An interface can't be instantiated directly.
  • Interfaces can contain events, indexers, methods, and properties.
  • Interfaces contain no implementation of methods.
  • A class can implement multiple interfaces but inherit only one abstract class.
  • Interfaces cannot contain fields

Difference between Abstract Class and an Interface

Abstract ClassInterface
A class may inherit only one abstract class.A class may inherit more than one interface.
An abstract class can have abstract or non-abstract methods.An interface cannot provide any implementation, only the signature.
An abstract class can contain access modifiers for members.An interface cannot have access modifiers. By default all members are public.
An abstract class can have fieldsInterfaces cannot contain fields