Interface Implementation in C#

  1. using System;  
  2.   
  3. namespace ConsoleApplication16  
  4. {  
  5.     public interface A  
  6.     {  
  7.         void Hello();   
  8.     }  
  9.     public interface B  
  10.     {  
  11.         void Hellonew();   
  12.     }  
  13.   
  14.     // Interface C inherite interface A and B     
  15.     //  C is a interface so can not define the function of interface A and B function inherted   
  16.     // Program  class we do implementation   
  17.   
  18.     public interface C : A , B  
  19.     {  
  20.         void HelloOld();   
  21.     }  
  22.   
  23.    public class Program : C  
  24.     {  
  25.        void A.Hello()   // this is the way to implement a function to reduce ambiguity bacuse may be other interface may have same function name  
  26.         {  
  27.           Console.WriteLine("Interface A is Called");  
  28.         }  
  29.         void B.Hellonew() // same as Interface A  
  30.         {  
  31.             Console.WriteLine("Interface B is Called");  
  32.         }  
  33.         void C.HelloOld()  // same as Interface A  
  34.         {   
  35.             Console.WriteLine("Interface C is Called");  
  36.         }  
  37.           
  38.     }  
  39.    class derived : Program  
  40.    {  
  41.        static void Main(string[] args)  
  42.        {  
  43.            derived obj = new derived();  // this is the way to call interface  function by derived class from Program class  
  44.              
  45.            ((C)obj).Hello();  
  46.            ((C)obj).Hellonew();  
  47.            ((C)obj).HelloOld();  
  48.   
  49.            C objnew = new Program(); // this is the way to call interface  function by Program class   
  50.              
  51.            objnew.Hello();  
  52.            objnew.Hellonew() ;  
  53.            objnew.HelloOld();  
  54.   
  55.            Console.ReadKey();  
  56.        }  
  57.      
  58.      
  59.    }  
  60. }  
Output would be same:
// Interface A is Called
// Interface B is Called
// Interface C is Called

// Interface A is Called
// Interface B is Called
// Interface C is Called