Understanding Interface in C#

What is an interface:

  • Interface contains definitions(only signatures) it does not contain any implementation details. it is very crispy.
  • Interface can inherit from another interface.
  • Interface can contain properties,method, events,indexers.
  • Interface can’t contain const,fields,operators,constructor and destructor.
  • Interface members are public by default.
  • Interface members can’t be static.
  • Interface can’t be instantiated directly.
  • Any number of interface can be applied on class or struct (i.e. Multiple interface can be implemented in single class or struct), but you can inherit only one class in another class.
  • Class or Struct which implement interface must provide implementation for all the implemented member.
  • Interface helps implementing multiple inheritance in C#
  • Interface helps implement inheritance in struct, since struct can’t inherit from another struct by default.

As you can see in the below example of interface definition, interface only contain the definition but it does not  have any idea about the implementation detail of “add”.

A class or struct which implement the specified interface supposed to give implementation details in the class or struct. so here I am writing a class which is going to inherit the interface which we defined above.

How to access the above class in your program,

Two ways of implementing an interface in c#:

  • Implicit Implementation
  • Explicit Implementation

I will write about in my next blog post, please feel free to provide your thoughts in comment section.

0 comments… add one

Leave a Comment