Multiple Interface Using Single Method

Today, I want to explain how to use two interface in single method as

Interface

Interface is nothing but contract that define signature of functionality. if a class implement interface
then it says to outer world that it provide specific behavior.

ex:

If a class implement 'IDisposable' interface that means it has functionality to release unused object (Unmanaged resource).

Declare Interface

  1. interface A1  
  2. {  
  3.    void write();  
  4.   
  5. }  
  6. interface A2  
  7. {  
  8.    void write();  
  9.   
  10. }   
Implement two Interface in Single Method
  1. class Program  
  2. {  
  3.    interface A1  
  4.    {  
  5.       void write();  
  6.   
  7.    }  
  8.    interface A2  
  9.    {  
  10.       void write();  
  11.   
  12.    }  
  13.   
  14.    public class A:A1,A2  
  15.    {  
  16.   
  17.       public void write()  
  18.       {  
  19.          Console.WriteLine("Hello Interface");  
  20.       }  
  21.   
  22.    }  
  23.   
  24.    static void Main(string[] args)  
  25.    {  
  26.   
  27.       A ab = new A();  
  28.       Console.WriteLine();  
  29.       ab.write();  
  30.       Console.ReadLine();  
  31.    }  
  32. }  
output

Above Interface example implement which interface method don't know. we want to call these two interface method then Tell compiler which interface method want to implement in such situation use interface name during implementation of a method.
  1. namespace TestVishu  
  2. {  
  3.    class Program  
  4.    {  
  5.       interface A1  
  6.       {  
  7.          void write();  
  8.   
  9.       }  
  10.       interface A2  
  11.       {  
  12.          void write();  
  13.   
  14.       }  
  15.   
  16.       public class A:A1,A2  
  17.       {  
  18.          void A1.write()  
  19.          {  
  20.             Console.WriteLine("Hello Interface A1");  
  21.          }  
  22.            
  23.          void A2.write()  
  24.          {  
  25.             Console.WriteLine("Hello Interface A2");  
  26.          }  
  27.   
  28.       }  
  29.   
  30.       static void Main(string[] args)  
  31.       {  
  32.          A1 ab = new A();  
  33.          Console.WriteLine();  
  34.          ab.write();  
  35.   
  36.          A2 ab2 = new A();  
  37.          Console.WriteLine();  
  38.          ab2.write();  
  39.          Console.ReadLine();  
  40.       }  
  41.    }  
  42. }  
console output

Ebook Download
View all
Learn
View all