Link to home
Start Free TrialLog in
Avatar of joshuadavidlee
joshuadavidlee

asked on

heres a tricky question on IDictionary.Contains method

i am making new Hashtable() objects all the time in my project.
however i am making them like so :

IDictionary id = new Hashtable();

my question is in regards to both the .Contains() method in IDictionary and the.ContainsKey() method in hashtable.
according to msdn on .ContainsKey() method:
>
This method behaves exactly as Contains.
This method is an O(1) operation.
>

which potentially means that it is faster.

what i want to know is if i use the .Contains() method will it use the underlining .ContainsKey method and therefore i will get O(1) operation? and can u prove this either way.
Avatar of AngryBinary
AngryBinary

While either a bechmark test or diassembly is the best way to PROVE this, I think the fact that an index lookup is a non-linear operation implies that any method that essentially and exclusively implements an index lookup (as opposed to an "iterate and search") will be an O(1) operation as well, and going the extra step to demonstrate proof of this is a little excessive unless you have a reeeeeeeeeeeally good reason to suspect otherwise :)

--Randall
I just looked into the assembly and I can confirm that Contains calls the ContainsKey method.
ASKER CERTIFIED SOLUTION
Avatar of ostdp
ostdp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial