locked
Implement Interface RRS feed

  • Question

  • Why would you implement an interface vs Implementing and interface explicitly?
    joe alt
    Tuesday, March 31, 2009 9:21 PM

Answers

  • Because if you implement an interface explicitly, you have to cast the object to the interface to call the method.

    Example stolen from Microsoft:

    // explicit1.cs
    interface IDimensions 
    {
       float Length();
       float Width();
    }
    
    class Box : IDimensions 
    {
       float lengthInches;
       float widthInches;
    
       public Box(float length, float width) 
       {
          lengthInches = length;
          widthInches = width;
       }
       // Explicit interface member implementation: 
       float IDimensions.Length() 
       {
          return lengthInches;
       }
       // Explicit interface member implementation:
       float IDimensions.Width() 
       {
          return widthInches;      
       }
    
       public static void Main() 
       {
          // Declare a class instance "myBox":
          Box myBox = new Box(30.0f, 20.0f);
          // Declare an interface instance "myDimensions":
          IDimensions myDimensions = (IDimensions) myBox;
          // Print out the dimensions of the box:
          /* The following commented lines would produce compilation 
             errors because they try to access an explicitly implemented
             interface member from a class instance:                   */
          //System.Console.WriteLine("Length: {0}", myBox.Length());
          //System.Console.WriteLine("Width: {0}", myBox.Width());
          /* Print out the dimensions of the box by calling the methods 
             from an instance of the interface:                         */
          System.Console.WriteLine("Length: {0}", myDimensions.Length());
          System.Console.WriteLine("Width: {0}", myDimensions.Width());
       }
    }
    In other words, it's a pain in the butt, so, if you don't need to, most people don't.

    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by altja Wednesday, April 1, 2009 1:38 AM
    Tuesday, March 31, 2009 10:24 PM
  • Sorry, I just realized I only answered part of your question:

    As my first post (and apparently second) said, implementing an interface normally (not explicitly) is a lot easier to deal with, both as the implementer and as the consumer of the object. However, you may run into a situation where you have to implement the interface explicitly, say if your are implementing two interfaces and they both have a method named "Foo()". In that case, I believe that you would have to implement the interface explicitly so as to remove any ambiguity.


    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by altja Wednesday, April 1, 2009 2:45 AM
    Wednesday, April 1, 2009 2:42 AM

All replies

  • Because if you implement an interface explicitly, you have to cast the object to the interface to call the method.

    Example stolen from Microsoft:

    // explicit1.cs
    interface IDimensions 
    {
       float Length();
       float Width();
    }
    
    class Box : IDimensions 
    {
       float lengthInches;
       float widthInches;
    
       public Box(float length, float width) 
       {
          lengthInches = length;
          widthInches = width;
       }
       // Explicit interface member implementation: 
       float IDimensions.Length() 
       {
          return lengthInches;
       }
       // Explicit interface member implementation:
       float IDimensions.Width() 
       {
          return widthInches;      
       }
    
       public static void Main() 
       {
          // Declare a class instance "myBox":
          Box myBox = new Box(30.0f, 20.0f);
          // Declare an interface instance "myDimensions":
          IDimensions myDimensions = (IDimensions) myBox;
          // Print out the dimensions of the box:
          /* The following commented lines would produce compilation 
             errors because they try to access an explicitly implemented
             interface member from a class instance:                   */
          //System.Console.WriteLine("Length: {0}", myBox.Length());
          //System.Console.WriteLine("Width: {0}", myBox.Width());
          /* Print out the dimensions of the box by calling the methods 
             from an instance of the interface:                         */
          System.Console.WriteLine("Length: {0}", myDimensions.Length());
          System.Console.WriteLine("Width: {0}", myDimensions.Width());
       }
    }
    In other words, it's a pain in the butt, so, if you don't need to, most people don't.

    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by altja Wednesday, April 1, 2009 1:38 AM
    Tuesday, March 31, 2009 10:24 PM
  • Sorry, I just realized I only answered part of your question:

    As my first post (and apparently second) said, implementing an interface normally (not explicitly) is a lot easier to deal with, both as the implementer and as the consumer of the object. However, you may run into a situation where you have to implement the interface explicitly, say if your are implementing two interfaces and they both have a method named "Foo()". In that case, I believe that you would have to implement the interface explicitly so as to remove any ambiguity.


    Chris Snyder, Stumbling through code one day at a time If your question was answered, please mark it.
    • Marked as answer by altja Wednesday, April 1, 2009 2:45 AM
    Wednesday, April 1, 2009 2:42 AM