1

I can hide a method in an implementing class as following using Explicit Interface:

interface IInterface
{
    void f1();

}
public class C1 : IInterface
{
    void IInterface.f1()
    {
        Console.WriteLine(" Method Called ....");
    }

}

Then could access the method:

C1 C = new C1();
IInterface i = C;
i.f1();

The method f1 will be hidden from the implementing class C1. What is the purpose of this kind of feature? Could anybody provide me an real life sceanrio where we require the same?

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
user1672097
  • 361
  • 1
  • 4
  • 12

4 Answers4

1

Here is a good example from MSDN, it helped my understanding why they could be helpful:

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());
   }
}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

There are scenarios when you don't want to expose something on the public API in the direct form. Maybe it will never work; maybe the public API can expose a more appropriate signature of the same functionality. For example:

public int Add(Foo foo) {...} // takes a known type and returns the new index
void ICollection.Add(object obj) {...} // takes object and returns void

A more common example is when it would be impossible not to - for example, IEnumerable and IEnumerable<T> each have a GetEnumerator() method - same inputs, but a different return value. C# does not allow overloading by return value, so you need at least two methods:

IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }

public IEnumerator<T> GetEnumerator() { ...}

although they could also both be explicit. Or indeed, in IEnumerator<T>:

public T Current { get { ... } }
object IEnumerator.Current { get { return Current; } }

Additionally, sometimes you just want to rename things. For example, for some reason a Frobber has a method which naturally is Frob(). But it also implements an IDoSomething interface which has a DoIt() method; you can now make the APIs reflect the most appropriate terminology to express intent:

class Frobber : IDoSomething {
  public void Frob() {..}
  void IDoSomething.DoIt() { Frob(); }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

You could check out the difference between implicit and explicit interface here:

C# Interfaces. Implicit implementation versus Explicit implementation

Community
  • 1
  • 1
Flagg1980
  • 295
  • 1
  • 2
  • 7
0

One of the real scenarios is the method name collision. Imagine:

interface IWarehouseItem
{
    string Name {get;} // the NAME of the PRODUCT
    int Count {get;}
    int PricePerUnit {get;}
}

interface IWarehouseSupplierInformation
{
    string Name {get;} // the NAME of the SUPPLIER
    int ID {get;}
    int IDAddress {get;}
}

class PurchaseableItem : Entity, IWarehouseItem, IWarehouseSupplierInformation
{
    ....
    // how to implement those different names?
     ....
    string IWarehouseItem.Name { get { this.Product.Name; } }
    ...
    string IWarehouseSupplierInformation.Name { get { this.Supplier.Name; } }
}

One may argue that this example is just a bad design of the interfaces and data structure, but not always you are able to make them proper, or have the power to change them at all.

Other times, you may be forced to implement some interface on a class, but you really really don't want those methods to be visible for the class' user. All explicit implementations are considered private, hence, unless you explicitely cast the object back to that offending interface, noone will ever see or be able to use those methods.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107