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

后端 未结 2 486
花落未央
花落未央 2021-01-21 03:12

In the following code, I am trying to check if two strings are anagrams. To that, I am counting the characters in the two strings in a hash table by storing the unique character

相关标签:
2条回答
  • 2021-01-21 03:34

    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.)

    0 讨论(0)
  • 2021-01-21 03:48

    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.

    0 讨论(0)
提交回复
热议问题