Welcome to RightHand's community place Sign in | | Help

Dictionary<> fun

Here is a brief trivia. Take a look at this piece of code:

Dictionary<Tubo, object> dictionary = new Dictionary<Tubo, object>(); Tubo tubo = new Tubo(); dictionary.Add(tubo, new object()); // some code here and there bool keyExists = dictionary.ContainsKey(tubo);

Basically I create a Dictonary instance and I use a class Tubo as a key type. I create an instance of Tubo (which is used as a key), add it to dictionary, run some code and then check whether key (same instance as above) is still there.

Is it possible that I get keyExists == false (note that Tubo instance is the same)?

Published 6. junij 2007 11:39 by Miha Markic
Filed under:

Comments

# re: Dictionary&lt;&gt; fun

6. junij 2007 13:53 by Petar Repac
Well, I get keyExists = True (as one would expect). You got False ? Regards, Petar

# re: Dictionary&lt;&gt; fun

6. junij 2007 14:28 by MatjažP
Hi, Miha. Answer: Yes it is possible! Welcome to the wonderful world of C++. It all depend on the equality operator. Are you comparing values or pointers? ;) I my be wrong, but on the other hand as you know I am not from the C# world, but from ancient world of C++, where we had STL (templates, dictionaries) some 10 years ago and we went on the same path as you are walking now. Have a nice day form Celje. Matjaž Prtenjak
Anonymous comments are disabled