Search code examples
vb.netdictionaryreferencekey

Is a VB.net dictionary's value returned by value or reference?


If I've got a dictionary and I get an item, is it's value or reference returned?

myDictionary.Add(myKey, myDictionaryObj)
Dim obj as myObject = myDictionary.Item(myKey)

will obj have the same value as myDictionaryObj or will it have it's reference?

Does it depend on the initial object?


Solution

  • You will get, what you have put into the dictionary.

    If the dictionary's type is a reference type (e.g. classes), you will get a reference to the object, because only the reference is stored in the dictionary.

    If the dictionary's type is a value type (e.g. structures), you will get a copy of the object, because the complete value is stored in the dictionary.