Let’s say we have this class:
public class Moo
{
string _value;
public Moo(string value)
{
this._value = value;
}
public static implicit operator string(Moo x)
{
return x._value;
}
public static implicit operator Moo(string x)
{
return new Moo(x);
}
public override bool Equals(object obj)
{
if (obj is string)
return this._value == obj.ToString();
else if (obj is Moo)
return this._value == ((Moo)obj)._value;
return base.Equals(obj);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
}
If I add an object of this type to a Hashtable, then I can find the object using the identical string.
But if the Hashtable contains the string not the object, it fails to find it when I pass the object.
Moo moo = new Moo("abc");
bool result1 = moo == "abc"; //true
bool result1a = moo.Equals("abc"); //true
bool result2 = "abc" == moo; //true
bool result2a = "abc".Equals(moo); //true
bool result2b = ((object)"abc").Equals(moo); //false
Hashtable hash = new Hashtable();
hash[moo] = 100;
object result3 = hash[moo]; //100
object result4 = hash["abc"]; //100
hash = new Hashtable();
hash["abc"] = 100;
object result5 = hash[moo]; //null!!!!
object result6 = hash["abc"]; //100
I’ve tried overriding operator== and it made no difference.
Is it possible to use implicit casting in a Hashtable in this way?
First, why are you using a hashtable rather than a generic
Dictionary<,>?Regardless, your problem is that the hashtable is calling
((object)"abc").Equals(moo), which returns false, of course. You can override that behavior by passing your own implementation ofIEqualityComparerto one of theHashTableconstructors with anIEqualityComparerparameter.Since, as you’ve noted, you are trying to avoid modifying legacy code, it may be that you are unable to replace the existing hash table with one that has a custom IEqualityComparer. If that’s true, then I think the answer is that you can’t do what you would like to do.
Since you’re developing this “moo” class, I suppose you have control over the code that passes the moo class to the hash table’s indexer. If that’s true, you could just call
ToString()on the object you’re passing to the indexer, and of course, overrideToStringinMoo.