Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Objects?

Tags:

c#

.net

vb.net

I just have to do a simple comparison i.e.

byte[] keya = System.Text.Encoding.ASCII.GetBytes("myFamilyColumn:1");
byte[] keyb = System.Text.Encoding.ASCII.GetBytes("myFamilyColumn:1");
Console.WriteLine(keya == keyb);

but the result is false, and their hash code is also different, am i missing something here?

Thanks in advance !

like image 903
Ali Avatar asked Jan 04 '11 21:01

Ali


People also ask

How do you compare two objects?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

What is a comparison object?

Object comparison refers to the ability of an object to determine whether it is essentially the same as another object. You evaluate whether one object is equal to another by sending one of the objects an isEqual: message and passing in the other object.

Is a function to comparing the objects?

Comparing objects is easy, use === or Object.is(). This function returns true if they have the same reference and false if they do not.

Can you use == with objects?

The == operator compares whether two object references point to the same object.


2 Answers

I don't believe there's anything in the framework which will give array equality in the way you'd want. However, it's not too hard to write your own implementation of IEqualityComparer<T> for arrays. For example (compiled but untested):

public static class ArrayEqualityComparer
{
    public static IEqualityComparer<T[]> Create<T>(
        IEqualityComparer<T> comparer)
    {
        return new ArrayEqualityComparer<T>(comparer);
    }
}

public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>
{
    private static readonly IEqualityComparer<T[]> defaultInstance = new
        ArrayEqualityComparer<T>();

    public static IEqualityComparer<T[]> Default
    {
        get { return defaultInstance; }
    }

    private readonly IEqualityComparer<T> elementComparer;

    public ArrayEqualityComparer() : this(EqualityComparer<T>.Default)
    {
    }

    public ArrayEqualityComparer(IEqualityComparer<T> elementComparer)
    {
        this.elementComparer = elementComparer;        
    }

    public bool Equals(T[] x, T[] y)
    {
        if (x == y)
        {
            return true;
        }
        if (x == null || y == null)
        {
            return false;
        }
        if (x.Length != y.Length)
        {
            return false;
        }
        for (int i = 0; i < x.Length; i++)
        {
            if (!elementComparer.Equals(x[i], y[i]))
            {
                return false;
            }
        }
        return true;
    }

    public int GetHashCode(T[] array)
    {
        if (array == null)
        {
            return 0;
        }
        int hash = 23;
        foreach (T item in array)
        {
            hash = hash * 31 + elementComparer.GetHashCode(item);
        }
        return hash;
    }
}

(Note that this currently assumes that elementComparer will cope with null values for both GetHashCode and Equals. The interface doesn't guarantee that, but the default equality comparers actually do handle it. You could modify the above code to be more robust, of course... I just don't have time right now.)

Usage:

IEqualityComparer<byte[]> x = ArrayEqualityComparer<byte>.Default;
bool equal = x.Equals(bytes1, bytes2);

IEqualityComparer<string[]> y = 
    ArrayEqualityComparer.Create(StringComparer.OrdinalIgnoreCase);
bool whatever = x.Equals(new[][] { "X", "Y" }, new[] { "x", "y" });
like image 102
Jon Skeet Avatar answered Sep 30 '22 16:09

Jon Skeet


keya and keyb are two entirely separate objects that just happen to contain the same bytes.

If you want to compare to see if two strings have the same characters, perhaps you should look at methods like String.Equals?

like image 35
Anon. Avatar answered Sep 30 '22 16:09

Anon.