• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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.
0
joshuadavidlee
Asked:
joshuadavidlee
  • 2
1 Solution
 
AngryBinaryCommented:
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
0
 
ostdpCommented:
I just looked into the assembly and I can confirm that Contains calls the ContainsKey method.
0
 
ostdpCommented:
This is the code that a decompiler produces:

public virtual bool Contains(object key)
{
      return this.ContainsKey(key);
}

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now