Two methods that offer the exact same functionality in the Dictionary class

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

For example the Hashtable implement the Contains method that Test whether a
specific key is contained in the collection.
In the Hashtable we have also the ContainsKey method that seems to be doing
the exact same thing i.e same functionality.

So why having two methods that offer the exact same functionality ?

//Tony
 
Tony said:
Hi!

For example the Hashtable implement the Contains method that Test whether a
specific key is contained in the collection.
In the Hashtable we have also the ContainsKey method that seems to be doing
the exact same thing i.e same functionality.

So why having two methods that offer the exact same functionality ?

You'll see that every now and then, where one method is the
implementation for some interface the object implements, while the other
method is just for the object class itself.

Of course, ideally one would not have the latter kind of method. The
interface implementation would always be used instead.

But a) sometimes a class implements a method explicitly, but still wants
some of the same functionality exposed through the object's actual
class, b) sometimes a class is updated to implement an interface it
didn't implement when it originally was created, and the interface has
some method duplicating functionality but with a different name, and c)
sometimes a class and interface are designed concurrently, but without
sufficient coordination, and some redundancy creeps in.

I suspect, but don't know for sure, that it's that latter issue that has
occurred in this particular example. The IDictionary interface and
Hashtable are, I'm pretty sure (and the docs say this is the case), both
from the original version of .NET, so it's not like the interface came
along later.

Pete
 
Back
Top