Solved

heres a tricky question on IDictionary.Contains method

Posted on 2006-11-07
3
463 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

770 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