14

Reading an article I came across the following C# syntax in method name.

private class sortYearAscendingHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
       ...
   }
}

I understand Compare method is method of IComparer interface, but coming from C++ I am not certain what this syntax means. If Compare is part of interface, I would expect to mention that only like int Compare(...). Why we have to specify class?

Pablo
  • 28,133
  • 34
  • 125
  • 215
  • 3
    This is known as an explicit interface implementation and it is not necessary in this case. You can use an an explicit interface implementation to differentiate a method that belongs to the implicit type/class interface and the *explicit* implemented interface. – User 12345678 Jul 21 '14 at 17:44
  • 4
    As pointed out by @TimCoker, the question here is "What is this notation" and not "What is Explicit implementation" and perhaps should be reopened. – dee-see Jul 21 '14 at 18:04
  • @Vache The other post answers this question, which is a suitable reason to close it as a duplicate. That this question didn't know what to call it, and therefore didn't find *the duplicate question* doesn't make the questions not duplicates. – Servy Jul 21 '14 at 18:36
  • Since the linking comment is no longer here, this question should be re-closed as a duplicate of: http://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation – Servy Jul 21 '14 at 18:38

1 Answers1

17

That is an explicit interface implementation You use it when you derive from multiple interfaces that contain similar (same signature) functions but need different implementations for each interface.

More information can be found on MSDN.

(Sample from linked page):

If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period. For example:

public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}

The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface. Both method implementations are separate, and neither is available directly on the class.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • This is a fine answer but what is the point? The question is a duplicate. – User 12345678 Jul 21 '14 at 17:49
  • 1
    @ByteBlast I didn't know it was a duplicate when I answered. Given that this question is about the syntax (not explicitly about implicit vs. explicit implementations) I'm not sure I agree that it *is* a duplicate. – BradleyDotNET Jul 21 '14 at 17:53
  • Let it be known that I do not mean any offence, I am only trying to understand the mindset behind answers in these scenarios because they always leave me with a o_O face. – User 12345678 Jul 21 '14 at 17:55
  • @ByteBlast no offence taken :). – BradleyDotNET Jul 21 '14 at 18:10