0

I have following piece of code and what I really don't understand is why I have two different references.

Dictionary<string, MyClass> tempDictionary = new Dictionary<string, MyClass>();
MyClass dbClass = db.Get<MyClass>().First();
MyClass localClass = null;

if(!tempDictionary.TryGetValue(dbClass.Id, out localClass))
{
    tempDictionary.Add(localClass.Id, localClass = dbClass); // <-- notice this assignment (localClass = dbClass)
}

var localRef = localClass.GetHashCode();  // 90107424
var dbRef = dbClass.GetHashCode();   // 91823173

Why those references are different? Shouldn't point to the same object?

Fka
  • 6,044
  • 5
  • 42
  • 60
  • If you want to know if two references are identical, `Object.ReferenceEquals` is the ticket. There's always a small but nonzero chance that someone incorrectly implements `.GetHashCode()` as non-deterministic. – Jeroen Mostert Nov 10 '16 at 14:54
  • How is the hash code constructed? And is there any deep copy issues going on? – Peter M Nov 10 '16 at 14:54
  • https://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx this article tells the reason I think. If you want them to return the same value then you can override GetHashCode for a class that you define. Also you can override Equals method also. Therefore you can compare two instances via their properties. – cCcik Nov 10 '16 at 14:55
  • @PatrickHofman - yes, so I'm going to fill it with records. Notice **!** sign. – Fka Nov 10 '16 at 14:56
  • 1
    If you forget about your `Dictionary` entirely and run this with just `localClass = dbClass`, do you still see the same? – James Thorpe Nov 10 '16 at 14:56
  • @PeterM - nobody changed default `GetHashCode` method. It's default one. – Fka Nov 10 '16 at 14:57
  • 1
    `localClass.Id` gives a NRE. – Patrick Hofman Nov 10 '16 at 14:57
  • But how can you get to assignment (localClass = dbClass)? If you are in that branch, localClass is null, and localClass.Id throws null reference, not? – Evk Nov 10 '16 at 14:57
  • 1
    Actually, shouldn't this code throw `NullReferenceException`? – James Thorpe Nov 10 '16 at 14:57
  • if the GetHashCode() is equal, it is not necessary for them to be the same – Vivek Nuna Nov 10 '16 at 14:58
  • 1
    You should double check this Q&A: http://stackoverflow.com/questions/720177/default-implementation-for-object-gethashcode – Matías Fidemraizer Nov 10 '16 at 14:58
  • @JamesThorpe - why? There's `out localClass`, so it isn't. – Fka Nov 10 '16 at 14:58
  • using `tempDictionary.Add(dbClass.Id, localClass = dbClass);` to avoid NRE I'm getting equal hashcodes – KMoussa Nov 10 '16 at 14:59
  • 1
    Because if `TryGetValue` returns false, the `out` param is set to the default value, in your case, `null`. – James Thorpe Nov 10 '16 at 14:59
  • @JamesThorpe - ahh. You're right but I assume that it never be (just for debug purposes). – Fka Nov 10 '16 at 15:00
  • 1
    All in all, it seems you may have oversimplified your problem, and there's something else going on that we can't see. – James Thorpe Nov 10 '16 at 15:01

0 Answers0