Solved

heres a tricky question on IDictionary.Contains method

Posted on 2006-11-07
3
441 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
Comment Utility
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
Comment Utility
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
Comment Utility
This is the code that a decompiler produces:

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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now