Static equals vs virtual equals

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Basically it does three things:

Check for reference equality (return true if so)
Check for reference nullity (return false if either value is null; by now the null == null case has been handled)
Check for value equality with first.Equals(second)
The ordering shouldn't matter if both values have well-behaved equality implementations, as equality should be implemented such that x.Equals(y) implies y.Equals(x). However, the offline documentation I've got installed does state that first.Equals(second) (or objA.equals(objB) to use the real parameter naming) is specified. The online documentation doesn't mention this, interestingly enough.

Just to make all of this concrete, the implementation could look like this:

public static bool Equals(object x, object y)
{
    if (x == y) // Reference equality only; overloaded operators are ignored
    {
        return true;
    }
    if (x == null || y == null) // Again, reference checks
    {
        return false;
    }
    return x.Equals(y); // Safe as we know x != null.
}


It must be highlighted that the main difference between the static Object.Equals and the virtualfirst.Equals(second) is that the latter will fail if first is a null reference



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ted at February 28, 2014 - 6:00 PM