Why is value comparison in hashtable returning false even when the values are the same?

落爺英雄遲暮 提交于 2019-12-01 22:22:35

The Hashtable class is not generic; it just contains Objects, not a specific type.

When you do this:

if (uniqueChars1[key] != uniqueChars2[key])

the compile-time type of uniqueChars[key] is Object, not Int32. So you're using the Object implementation of the inequality operator, which just compares references. Since Int32 is a value type, and the indexer returns an object, the value is boxed; and since you're boxing two values, you get two distinct object instances, so reference equality always returns false.

You have several options:

  • use a Dictionary<char, int>, which is the generic equivalent of Hashtable
  • cast the values to int before comparison:

    if ((int)uniqueChars1[key] != (int)uniqueChars2[key])
    
  • use the Equals method to compare values:

    if (!uniqueChars1[key].Equals(uniqueChars2[key]))
    

Unless you're still using .NET 1.x, I strongly advise you to use generic collections everywhere you can. They're safer, more intuitive, and have better performance for value types.


Side note (unrelated to your problem): you don't need to pass the Hashtable by reference to AddToHashTable; the code would work exactly the same way without the ref modifier, because Hashtable is a reference type, so it's always a reference that is passed anyway. The ref modifier would only be useful if you were assigning something else to the uniqueChars parameter, or if you were passing a value type and mutating its state (which is usually consider a bad thing). I suggest you read Jon Skeet's great article about value types, reference types, and parameter passing.

Your problem comes from the fact that != will compare reference equality on two objects. uniqueCharsX[key] returns a int boxed inside a object, while you have the same int being returned from both hashtables the box they are being returned in is not the same box so you get a incorrect value.

Either use a strongly typed Dictionary<char, int> instead of a hashtable or use !uniqueChars1[key].Equals(uniqueChars2[key]) instead of uniqueChars1[key] != uniqueChars2[key] which will unbox the int and compare on the value instead (I highly recommend you use the Dictionary.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!