Explicit interface in C#

You have used millions of times, but you maybe don’t know what they call it (I just saw it and I didn't know too). Suppose these two interfaces with a member Called WithDraw():

 

  1.  public interface IBankingA
  2.     {
  3.         void WithDraw();
  4.     }
  5.  
  6.     public interface IBankingB
  7.     {
  8.         void WithDraw();
  9.     }

Now If you want to implement the members of these two interfaces in a class, you should tell the compiler that I'm implementing the members of both interfaces as they have same name by typing the interface name before the method name like this:

  1.  public class Baking :IBankingA, IBankingB
  2.     {
  3.         void IBankingA.WithDraw()
  4.         {
  5.             throw new NotImplementedException();
  6.         }
  7.  
  8.         void IBankingB.WithDraw()
  9.         {
  10.             throw new NotImplementedException();
  11.         }
  12.     }

That's it!


Tags: C#


comments powered by Disqus