Hash Table Question

Hi,

what I need is as follow. I have a class that contains a string.

ref class WordObj
{
public:
String^ string;
Int32 info;
// further data
};

Now I want to add such WordObj to a hashtable.

myHT.add(wo->string, wo);

As you can see, the string inside the WordObj is the key. Is it possible to prevent the hashtable object to store the string twice Once as key and a second time as part of the stored object. It is important, that the string stays part of the object.

Thank you,

Bernd



Answer this question

Hash Table Question

  • Joe Dawson

    String class in .NET is immutable. It means that it can not be modified. You can not change the value of it, you create a new String instance instead.

    If the strings can not be modified it can reuse the same string data.

    However, a Hashtable will keep a copy of the string so if you modify it in the original structure it is part of it will not change the key for the entry in the Hashtable.



  • AlexY

    Thank you.


  • Raghuram_816

    Moved to VC++ EE



  • Hash Table Question