Solved

heres a tricky question on IDictionary.Contains method

Posted on 2006-11-07
3
468 Views
Last Modified: 2008-01-09
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
Comment
Question by:joshuadavidlee
  • 2
3 Comments
 
LVL 13

Expert Comment

by:AngryBinary
ID: 17900495
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
 
LVL 4

Expert Comment

by:ostdp
ID: 17900905
I just looked into the assembly and I can confirm that Contains calls the ContainsKey method.
0
 
LVL 4

Accepted Solution

by:
ostdp earned 500 total points
ID: 17900913
This is the code that a decompiler produces:

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

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question