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
- interface A1
- {
- void write();
-
- }
- interface A2
- {
- void write();
-
- }
Implement two Interface in Single Method
- class Program
- {
- interface A1
- {
- void write();
-
- }
- interface A2
- {
- void write();
-
- }
-
- public class A:A1,A2
- {
-
- public void write()
- {
- Console.WriteLine("Hello Interface");
- }
-
- }
-
- static void Main(string[] args)
- {
-
- A ab = new A();
- Console.WriteLine();
- ab.write();
- Console.ReadLine();
- }
- }
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.
- namespace TestVishu
- {
- class Program
- {
- interface A1
- {
- void write();
-
- }
- interface A2
- {
- void write();
-
- }
-
- public class A:A1,A2
- {
- void A1.write()
- {
- Console.WriteLine("Hello Interface A1");
- }
-
- void A2.write()
- {
- Console.WriteLine("Hello Interface A2");
- }
-
- }
-
- static void Main(string[] args)
- {
- A1 ab = new A();
- Console.WriteLine();
- ab.write();
-
- A2 ab2 = new A();
- Console.WriteLine();
- ab2.write();
- Console.ReadLine();
- }
- }
- }