What is a Interface in C# .Net

Interface is a type which contains only the signatures of methods, delegates or events, it has no implementation.
Implementation of the methods is done by the class which implements the interface.

Interface is a contract that defines the signature of the functionality. So if a class is implementing an interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.

If a class implements an interface then it has to provide implementation to all its methods.

Properties Of Interface:-

  • Supports multiple inheritance(Single Class can implement multiple interfaces).
  • Contains only incomplete method.
  • Can not Contains data members.
  • By Default interface members is public (We Can not set any access modifier into interface members).
  •  Interface can not contain constructors.

Example :- Defining an Interface:

     interface IApp
     {
       int Sum(int i, int j); //By Default Public
     }

     class App : IApp
     {
        public int sum(int i, int j) //Must be declare Public
        {
           return i + j;
        }
    }

Recent Posts