Interface in C#

Interface is a set of similar functionalities that can be implemented by a class or struct. You can think it as a contract in which it is said that any class or struct that is implementing it must provide implementation of all the methods, properties, indexers and events, declared in it.

Syntax:
interface MyApp
{
int play(int x, int y);
}

Now let us discuss it in detail.

Interfaces is one of the most important topic. It does not know anything about the implementation. It contain only the signatures of methods, properties, indexers or events. As we know that in C# Multiple Inheritance is not allowed as it was there in C++. So there should be a way to use / inherit functionality or behaviour of more than one class. Here comes Interface. It acts as a replacement of Multiple Inheritance.



Summarization – Interface

  • It supports Multiple Inheritance.
  • We cannot create instance of Interface. See Detail
  • As per naming convention interfaces starts with letter ‘I’.
  • It is a pure abstract class.
  • Constructors cannot be created in it. See Detail
  • Data Members are not allowed in Interfaces. See Detail
  • Default access specifier of Interface members is ‘public’. Not allowed to specify any access modifiers. See Detail
  • Class and struct can implement one or more Interfaces.

Interface supports Multiple Inheritance

With the help of Interfaces, multiple Inheritance is possible in C#.
class MyProgram : IDisposable, IFormattable
Here MyProgram Class implements two interfaces.

Direct Instance of Interface cannot be created

ImyApp myApp = new ImyApp(); Incorrect Code

But you can do indirectly by creating reference of Interface.
ImyApp myapp = new myClass();

Here ImyApp -> Interface
myClass -> Class Name
Interface
Related Question : How can we call a method in class if 2 interfaces it implements contains the same method name with same signature ?

Constructors are not allowed in Interface.

Interface

Data Members are not allowed in Interfaces.

You cannot define data fields in the Interface.
See figureInterface

No access Specifier is allowed with data members of Interfaces

As the chief motive of Interfaces is Inheritance thats why the access modifier of its members functions is public by default. So you cannot put private/ protected or any other access modifier not even public while declaring methods in the Interface.

Interface

Problem : How can we call a method in class if 2 interfaces it implements contains the same method name with same signature ?

Solution : Suppose we have 2 interfaces ICalculator and IArea.
Explicit Interface

Now lets make a class and implement the above 2 interfaces.
public class Program : ICalculator, IArea
{
public void print()
{
//only one code block is allowed
}

So 2nd technique is
public class Program : ICalculator, IArea
{
void ICalculator.print()
{
Console.WriteLine("I am method of ICalculator Interface");
}
void IArea.print()
{
Console.WriteLine("I am method of IArea Interface");
}

So we gave defination to both of the methods above. Now let us try to call the methods.
Explicit Interface

It is clear from the above code that after creating object ‘obj’ of class, we are not able to access its print method. Why?? It is because the compiler could not resolve which print method to call we have 2 methods in our class. This problem is called as Ambiguity. So here required explicit method implementation. which can be achieved through Interface reference. Let us find out how.
Interface
Output