Interface Best Example in C#

This article explains the following:

  • Why to use an interface in C#?
  • What is an interface?
  • What is the best example of an interface?

Why to use an interface

I hope everyone is quite familiar with OOP concepts in C++. (Class and Object, Inheritance and its type, so on).

Inheritance allows creating classes that are derived from other classes, so that they automatically include some of its "parent's" members, plus its own. The following are types of inheritances.

Multi-Level Inheritance

  1. class Father {  
  2.     //Father Relations--Members  
  3. }  
  4. class Mother: Father {  
  5.     //Mother Relations--Members  
  6. }  
  7. class Child: Mother //Multi- Level Inheritance  
  8. {  
  9.     //Accessing both Relations class members.  
  10. }  

Multiple Inheritance

  1. class Father {  
  2.     //Father Relations--Members  
  3. }  
  4. class Mother {  
  5.     //Mother Relations--Members  
  6. }  
  7. class Child: Mother, Father //Multiple Inheritance  
  8. {  
  9.     //Accessing both Relations class members.  
  10. }  

This type of multiple inheritance is possible in C++ but it's not possible in C#.

SS.jpg

What is Interface?

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by structs and classes, that must provide an implementation for each interface member declared.

So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way.

Because interfaces must be implemented by derived structs and classes, they define a contract. 

  1. interface IMyInterface {  
  2.     void MethodToImplement(); //Abstract Method signature.  
  3. }  
  4. class InterfaceImplementer: IMyInterface {  
  5.     static void Main() {  
  6.         InterfaceImplementer obj = new InterfaceImplementer();  
  7.         obj.MethodToImplement();  
  8.     }  
  9.     public void MethodToImplement() {  
  10.         //Abstract Method Implementation  
  11.     }  
  12. }  

Here, we advised to use "I" as the prefix for the interface to understand that the interface is an interface.

Best example for Interface

Here I have the best example for understanding interfaces.

Not Possible:
InterF.jpg
Open a Console Application and give "InterFaceDemo" as the project name, then add a new class item and rename it "ODDEVEN.CS".

ODDEVEN.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InterFaceDemo {  
  6.     interface IOne {  
  7.         void ONE(); //Pure Abstract Method Signature  
  8.     }  
  9.     interface ITwo {  
  10.         void TWO();  
  11.     }  
  12.     interface IThree: IOne {  
  13.         void THREE();  
  14.     }  
  15.     interface IFour {  
  16.         void FOUR();  
  17.     }  
  18.     interface IFive: IThree {  
  19.         void FIVE();  
  20.     }  
  21.     interface IEVEN: ITwo, IFour {}  
  22.     class ODDEVEN: IEVEN, IFive //Must Implement all the abstract method, in Derived class.  
  23.     {  
  24.         public void ONE() //Implementation of Abstract Method.  
  25.         {  
  26.             Console.WriteLine("This is ONE");  
  27.         }  
  28.         public void TWO() {  
  29.             Console.WriteLine("This is TWO");  
  30.         }  
  31.         public void THREE() {  
  32.             Console.WriteLine("This is THERE");  
  33.         }  
  34.         public void FOUR() {  
  35.             Console.WriteLine("This is FOUR");  
  36.         }  
  37.         public void FIVE() {  
  38.             Console.WriteLine("This is FIVE");  
  39.         }  
  40.     }  
  41. }  

Program.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace InterFaceDemo {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             Console.WriteLine("This is ODD");  
  9.             IFive obj1 = new ODDEVEN();  
  10.             obj1.ONE();  
  11.             obj1.THREE();  
  12.             obj1.FIVE();  
  13.             Console.WriteLine("\n\nThis is EVEN");  
  14.             IEVEN obj2 = new ODDEVEN();  
  15.             obj2.TWO();  
  16.             obj2.FOUR();  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  

The following is the output:

sss.jpg

Enjoy Coding.

Up Next
    Ebook Download
    View all
    Learn
    View all