Please enable Javascript to correctly display the contents on Dot Net Tricks!

A Deep Dive into C# Interface

  Author : Shailendra Chauhan
Updated On : 01 Nov 2013
Total Views : 108,894   
Support : All C#
 

An interface acts as a contract between itself and any class or struct which implements it. It means a class that implement an interface is bound to implement all its members. Interface has only member’s declaration or signature and implicitly every member of an interface is public and abstract.

For example, the most common use of interfaces is, within SOA (Service Oriented Architecture). In SOA (WCF), a service is exposed through interfaces to different clients. Typically, an interface is exposed to a group of clients which needs to use common functionalities.

interface IStore
{
 void Read();
 void Write();
}

interface ICompress
{
 void Compress();
 void Decompress();
}

public class Document : IStore, ICompress
{
 #region IStore

 public void Read()
 {
 Console.WriteLine("Executing Document's Read Method for IStore");
 }

 public void Write()
 {
 Console.WriteLine("Executing Document's Write Method for IStore");
 }

 #endregion // IStore

 #region ICompress

 public void Compress()
 {
 Console.WriteLine("Executing Document's Compress Method for ICompress");
 }
 public void Decompress()
 {
 Console.WriteLine("Executing Document's Decompress Method for ICompress");
 }
 #endregion // ICompress
} 

Features of Interface

  1. An interface doesn't provide inheritance like a class or abstract class but it only declare members which an implementing class need to be implement.

  2. It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.

    IStore IObjStore = new Document();
    ICompress IObjCompress = new Document();
    
  3. It contains only properties, indexers, methods, delegates and events signature.

  4. It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.

  5. Members of an interface cannot have any access modifiers even public.

  6. Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual.

  7. An interface can be inherited from one or more interfaces.

  8. An interface can extend another interface.

  9. A class or struct can implements more than one interfaces.

  10. A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.

  11. Implementing multiple interfaces by a class, sometimes result in a conflict between member signatures. You can resolve such conflicts by explicitly implementing an interface member.

    interface IStore
    {
     void Read();
     void Write();
    }
    
    interface ICompress
    {
     void Compress();
     void Decompress();
    }
    
    public class Document : IStore, ICompress
    {
     #region IStore Explicit Implementation
    
     public void IStore.Read()
     {
     Console.WriteLine("Executing Document's Read Method for IStore");
     }
    
     public void IStore.Write()
     {
     Console.WriteLine("Executing Document's Write Method for IStore");
     }
    
     #endregion // IStore
    
     #region ICompress Implicit Implementation
    
     public void Compress()
     {
     Console.WriteLine("Executing Document's Compress Method for ICompress");
     }
     public void Decompress()
     {
     Console.WriteLine("Executing Document's Decompress Method for ICompress");
     }
     #endregion // ICompress
    }
    
  12. It is a good practice to start all interface names with a capital “I” letter.

Common design guidelines for Interface

  1. Keep your interfaces focused on the problem you are trying to solve and keep related tasks (methods) into a interface. Interfaces that have multiple unrelated tasks tend to be very difficult to implement in a class. Split up interfaces that contain unrelated functionality.

  2. Make sure your interface does not contain too many methods. Since too many methods makes implementing the interface difficult as the implementing class has to implement each and every method in the interface.

  3. Don't make interfaces for specific functionality. An interface should define the common functionality that can be implemented by the classes of different modules or subsystems.

When to use

  1. Need to provide common functionality to unrelated classes.

  2. Need to group objects based on common behaviors.

  3. Need to introduce polymorphic behavior to classes since a class can implements more than one interfaces.

  4. Need to provide more abstract view to a model which is unchangeable.

  5. Need to create loosely coupled components, easily maintainable and pluggable components (like log4net framework for logging) because implementation of an interface is separated from itself.

Disadvantage of Interface

  1. The main issue with an interface is that when you add a new members to its, then you must implement those members within all of the classes which implement that interface.

  2. Interfaces are slow as these required extra in-direction to find corresponding method in in the actual class.

What do you think?

I hope, now you have got everything about Interface. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
11 DEC
ASP.NET MVC with AngularJS Development (offline)

Sat-Sun 09:30 AM-11:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (11:00 AM-12:30 PM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+