User Id :    Password :      New Member   Forgot Password  
Topic: Explicit interface implementation?
Anamika singh
22 Nov 2010 at 11:36 PM
We have interface A and Interface B both have same function by the name Sum(). When a class inherit that interfaces by class abc: A,B then tell me how we know that which function is calling?( Explicit interface implementation)
Re: Explicit interface implementation?
Gaurav
16 Dec 2010 at 04:30 AM
Explicit interface implementation also allows the programmer to inherit two interfaces that share the same member names and give each interface member a separate implementation.

This example displays the dimensions of a box in both metric and English units. The Box class inherits two interfaces IEnglishDimensions and IMetricDimensions, which represent the different measurement systems. Both interfaces have identical member names, Length and Width.


// explicit2.cs
// Declare the English units interface:
interface IEnglishDimensions
{
   float Length();
   float Width();
}
// Declare the metric units interface:
interface IMetricDimensions
{
   float Length();
   float Width();
}
// Declare the "Box" class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
   float lengthInches;
   float widthInches;
   public Box(float length, float width)
   {
      lengthInches = length;
      widthInches = width;
   }
// Explicitly implement the members of IEnglishDimensions:
   float IEnglishDimensions.Length()
   {
      return lengthInches;
   }
   float IEnglishDimensions.Width()
   {
      return widthInches;     
   }
// Explicitly implement the members of IMetricDimensions:
   float IMetricDimensions.Length()
   {
      return lengthInches * 2.54f;
   }
   float IMetricDimensions.Width()
   {
      return widthInches * 2.54f;
   }
   public static void Main()
   {
      // Declare a class instance "myBox":
      Box myBox = new Box(30.0f, 20.0f);
      // Declare an instance of the English units interface:
      IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
      // Declare an instance of the metric units interface:
      IMetricDimensions mDimensions = (IMetricDimensions) myBox;
      // Print dimensions in English units:
      System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
      System.Console.WriteLine("Width (in): {0}", eDimensions.Width());
      // Print dimensions in metric units:
      System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
      System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
   }
}
Output
Length(in): 30
Width (in): 20
Length(cm): 76.2
Width (cm): 50.8


Total Replies: 1
 
Featured Text Ad

Featured Ad