1

I noticed that ConcurrentDictionary implements the IDictionary interface, yet despite that the interface supports Add, ConcurrentDictionary doesn't have that function. How does this work? I thought interfaces imposed functionality on the implementing classes...

Sean
  • 2,531
  • 2
  • 17
  • 17

1 Answers1

1

It is using explicit interface implementation. Here is an example.

interface IFoo
{
   void Foo();
}

class FooImplementation : IFoo
{
   void IFoo.Foo()
   {
   }
}

If you assign or cast a ConcurrentDictionary to IDictionary, you can use all the methods defined there.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I figured it was something like this, though when I 'go to definition' of ConcurrentDictionary to view the Metadata breakdown of the class, it doesn't show any of the explicit methods' signatures. Where are the explicit methods defined? My understanding is that all the methods of an interface need to be implemented whether implicit or explicit, so this suggests to me that I should still see Add in the CD Metadata. Perhaps the Metadata is incomplete? Also I'm assuming that these explicit methods would map for example: IDictionary.Add to ConcurrentDictionary.TryAdd; is this correct? – Sean Dec 26 '12 at 14:30