What is Interface in C#.net ?

By | December 10, 2017

What is Interface in C#.net

Interface is a type which contains only the signatures/declaration of methods/functions, delegates or events.

Declaration /signature of methods meaning method having no implementation as follows,

void Display(string name);  //By Default interface member is Public

It does not contain implementation of methods. Implementation of the methods is done by the class that which implements the interface.

Interface is a contract that defines the signature of the functionality. So if a class is implementing
a 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. Now external objects using this class know that it has contract by which it can dispose Un-used unmanaged objects.

Important points to remember about Interface:

a) Supports multiple interface (Single Class can implement multiple interfaces.)

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

c) Contains declaration / signature of methods only, it does not contain implantation of methods.

d) It cannot contain Data members eg.:

string name

e) By Default interface members are Public (We cannot set any access modifier to interface members).

f) Interface cannot contain constructors.

Example: – Defining an Interface,

     interface IDisplay
     {
        string name(string name); //By Default interface members are Public

         int a ; // Error, It cannot contain Data members .  
     }

     class Demo : IDisplay
     {
        public string  name(string name) //Must be declare Public
        {
            return “” + name;
        }
     }

 

c# interview questions

Leave a Reply

Your email address will not be published. Required fields are marked *