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)?

2 thoughts on “Dictionary<> fun

  1. 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

Leave a Reply