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
The Hashtable
class is not generic; it just contains Object
s, 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:
Dictionary
, 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.