0

I'm going mad with value vs reference in relation to dictionaries.

How come when I do this:

Object obj = ObjDictionary[key];
obj.intproperty ++;

C# increments the intproperty of the corresponding object in ObjDictionary as well as obj itself. This suggests to me the operation creates a reference rather than a copy of the Object but I can't find out how to copy the Object itself into a new one.

This is causing all sorts of havoc for me as I try to create new dictionaries from objects copied from the original dictionary.

EDIT:

I understand the dictionary does not contain objects, but references to objects.

The problem I'm still having is how this line works.

Object obj = ObjDictionary[key];

How does it compile when obj is an Object and ObjDictionary[key] is a reference? Is this something C# does implicitly?

Chris
  • 5
  • 4
  • What is `Object` here? Obviously it's not `System.Object`, since that type doesn't have `intproperty`. – Peter Duniho Jul 13 '15 at 03:59
  • Can you share some more specific example code that's vexing you? The sample you've provided now isn't valid C#, since the compiler doesn't know that `intproperty` exists on all objects. – Cameron Jul 13 '15 at 03:59
  • sorry no, it's a custom object with a property of type int – Chris Jul 13 '15 at 03:59
  • This is simple. When you set object to the object in the dictionary it references it. To copy the object use dictionary[key].copyto or the clone method. – deathismyfriend Jul 13 '15 at 04:03
  • The duplicate is helpful thank you and confirms what I knew about the Dictionary containing references to Objects, rather than the Objects themselves. How do I use the copyto or clone method? I can't get it to play the game. Also, I still struggle to understand how `Object obj = Object obj =ObjDictionary[key]` compiles when obj is supposed to _be_ an object, bot a reference to one? – Chris Jul 13 '15 at 04:03
  • **Careful.** What the dictionary contains depends not on the dictionary object itself, but the type of object. Please read up on the difference between `class` (reference types) and `struct` (value types). – Peter Duniho Jul 13 '15 at 04:04
  • I thought http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically would be more appropriate for what you trying to do, but one found by @PeterDuniho is actually better match to how question is asked. – Alexei Levenkov Jul 13 '15 at 04:04
  • @PeterDuniho it contains Class types. Thanks – Chris Jul 13 '15 at 04:07
  • @AlexeiLevenkov I looked at the deep copy solution but I guess what's throwing me is the `Object obj =ObjDictionary[key]` line. Doesn't seem to me like it should compile as `ObjDictionary[key]` is returning a reference, not an object? – Chris Jul 13 '15 at 04:08
  • 1
    _`Object obj = Object obj =ObjDictionary[key]`_ won't compile under any circumstance. In any case, if you go back and actually understand the difference between reference types and value types, some of this will make more sense. The basic issue is that you are thinking that the variable contains the object itself, but that's true only for value types. For reference types, the variable _always_ contains only a reference to the object. This is true whether the variable is a class field, a local variable, or an element of a collection like a dictionary or list. – Peter Duniho Jul 13 '15 at 04:11
  • @PeterDuniho should make that an answer and get some points :) – Chris Jul 13 '15 at 04:15
  • @Chris: I'm happy to answer that _different_ question if the OP decides to post it as an actual different question. I certainly would not want to do something so silly as to post an answer to a question that is a duplicate of one or more other questions. :) – Peter Duniho Jul 13 '15 at 04:16
  • @PeterDuniho I appreciate the answer regardless. I had looked at all the related concepts but was still missing your point: _For reference types, the variable always contains only a reference to the object. This is true whether the variable is a class field, a local variable, or an element of a collection like a dictionary or list_ – Chris Jul 13 '15 at 04:19
  • Happy I could help. :) – Peter Duniho Jul 13 '15 at 04:50

0 Answers0