25

I have a Dictionary<int, Product>. If the same Product is added to more than one key is an new instance of that object stored for each key? Or just a reference to the original object?

This collection is very large and each product will have between 1-10 Keys to it. My primary concern is memory allocation.

NSjonas
  • 10,693
  • 9
  • 66
  • 92

4 Answers4

38

If Product is a reference type (class and not struct), only a reference will be stored.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • what if its a tuple? – NSjonas Oct 24 '12 at 22:43
  • So if I load a large list from the database and then create 3 different dictionaries in order to index on different variables then this would not be memory size * 4 (list, 3 dictionaries)? – MIKE Sep 13 '17 at 16:28
5

No, it should use the same reference to the original object.

I'm not entirely certain how it will behave if the Dictionary is serialized/deserialized, however.

Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • You cannot serialize/deserialize a dictionary unless you write your own logic to handle that, as it is not supported by default. – eandersson Oct 24 '12 at 22:47
  • 3
    The above comment may or may not have been true 2 years ago, but it is not true now. The standard .NET serializer/deserializer seems to handle dictionaries quite nicely, at least in .NET 4.0 and above. – DaveN59 Oct 03 '14 at 19:36
5

The Dictionary will store a copy of the the value of the key that you pass it. It wouldn't be possible for it, or any other collection/container for that matter, to store a reference to any value as it is possible for the container to outlive the variable that you tried to store in it.

Now, as others have said, if the type of the value is a reference type, the value of the variable is just a reference, so you're just storing a copy of the reference to the variable. If the Type of the value of the dictionary is a value type then the actual value will be copied.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Servy
  • 202,030
  • 26
  • 332
  • 449
  • if key and value is class then both are referenced not COPY – Sebastian 506563 Jul 13 '14 at 10:31
  • 1
    @Taumantis As I mention in my answer itself, if the variable is a reference type then you are storing a *copy of the reference*. That is *not* storing a reference to the variable. It is *incorrect* to say that the value is copied by reference, or that a reference to the *variable* is stored. The value of the variable is copied, whatever that value is. If that value is itself a reference, then that's what is *copied*. – Servy Jul 14 '14 at 14:10
0

reference types are stored as references always. no one is going to guess what "clone" logic you intend for your type. if you need copies, you will have to create them on your own before placing to containers, passing to other functions and so on.

value types are copied (simplistically byte representation copy is created, however all reference values will still be references), unless passed as ref to functions. for containers though they will be copies. unless you create a reference type wrapper for them.

aiodintsov
  • 2,545
  • 15
  • 17