Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit casting of an object to string, for use in a Hashtable

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?

like image 922
demoncodemonkey Avatar asked Mar 07 '12 16:03

demoncodemonkey


2 Answers

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 of IEqualityComparer to one of the HashTable constructors with an IEqualityComparer parameter.

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, override ToString in Moo.

like image 186
phoog Avatar answered Sep 20 '22 12:09

phoog


So the implicit casts there are irrelevant. They won't do a thing. You'll get exactly the same results if you comment them out. What matters here is the Equals and GetHashCode methods.

So your GetHashCode method is fine. Two objects (whether it's a string or a moo) will always have the same hash if they should be the same, so it will work. Your problem is with your equals method.

moo.Equals("abc") will return true.

object obj = moo;
"abc".Equals(obj)

will return false; "abc".Equals(moo) will return true only because there is an implicit operator that converts moo into a string, and uses the string overload of Equals, rather than the object overload of Equals. Since the Hashtable is going to store everything as an object, it will always be using the object overload of the Equals method. (The == operator isn't used; it uses the Equals method within dictionary.) This is because the string's Equals method will check to make sure that the object passed in is a string, and it will return false if its not.

The solution: You need to not use the default constructor of HashTable. What you need to do is use a custom IEqualityComparor so that it doesn't use the Equals method of the object itself. This is not too hard to do. Just make a new class that extends IEqualityComparer and give it an equals method that is basically the same as your moo's Equals method so that it will perform the comparison properly regardless of which type either object it.

like image 21
Servy Avatar answered Sep 21 '22 12:09

Servy