比较对象? [英] Compare Objects?

查看:171
本文介绍了比较对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是做一个简单的比较,即。

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);

但结果是假的,他们的哈希code也是不同的,我失去了一些东西呢?

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

在此先感谢!

推荐答案

我不相信有一个在这将给阵列平等你想要的方式的框架事情。但是,这不是太难写自己的实现的IEqualityComparer&LT的; T> 的阵列。例如(编译但未经测试):

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;
    }
}

(注意,这个当前假设 elementComparer 将应付为空值 GetHash code 等于,接口不保证,但默认平等comparers实际的的处理。你可以修改上面的code到更加强劲,当然......我只是没有时间现在。)

(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.)

用法:

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" });

这篇关于比较对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆