Strongly-Typed Equals and Compare Static Methods

Problem
You’re looking for a simplified way to compare instances of custom types.

Solution
Provide static, strongly-typed versions of the Compare and Equals methods.

Comments
You can make your code more robust by using methods that take arguments of a specific type. You should avoid taking in generic Object instances. Static methods are often preferable to instance ones. This is because you don’t need to check whether an object reference is null before calling the method.

Example
The String class exposes two overloads of the Equals static method. There’s the one inherited from Object, which takes two object arguments. There’s also the strongly-typed one that takes two strings. The String class also exposes several overloads of the Compare static method. These methods allow developers to test an entire string or a portion of it in both case-sensitive and case-insensitive modes.

public class Account {
    // In a real-world class, these would be properties.
    public string AccountName;
    public string AccountType;

    public Account(string accountName, string accountType) {
        this.AccountName = accountName;
        this.AccountType = accountType;
    }

    // The strongly-typed version of the static Equals method.
    public static bool Equals(Account objectOne, Account objectTwo) {
        if ( objectOne == null && objectTwo == null ) {
            // Two null objects are equal.
            return true;
        }
        else if ( objectOne == null || objectTwo == null ) {
            // They can't be equal if only one object is null.
            return false;
        }
        else {
            // Delegate to the String.Equals method if neither is null.
            return ( string.Equals(objectOne.AccountName, objectTwo.AccountName) &&
                String.Equals(objectOne.AccountType, objectTwo.AccountType) );
        }
    }

    // The strongly-typed version of the static Compare method.
    // Here we compare accounts on (AccountType, AccountName).
    public static int Compare(Account objectOne, Account objectTwo) {
        if ( objectOne == null && objectTwo == null ) {
            // Two null objects are equal.
            return 0;
        }
        else if ( objectOne == null ) {
            // A null object is less than any non-null object.
            return -1;
        }
        else if ( objectTwo == null ) {
            // A non-null object is greater than any null object.
            return 1;
        }
        else {
            // Delegate comparison to the String.Compare method.
            int result = String.Compare(objectOne.LastName, objectTwo.LastName);
            if ( result == 0 ) {
                result = String.Compare(objectOne.FirstName, objectTwo.FirstName);
                return result;
            }
        }
    }
}

// You can simplify the Equals method if a type exposes a static
// Compare method and performance is a non-issue.
public static bool Equals( Account objectOne, Account objectTwo) {
    return Compare(objectOne, objectTwo) == 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s