在C#两个结构的平等 [英] Equality of two structs in C#

查看:94
本文介绍了在C#两个结构的平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找这个结构的两个实例之间的平等

 公共结构意甲< T> 
{
T [] X;
双击[〕Y;

公共意甲(T []×,双[] Y)
{
X = X;
Y = Y;
}

公众覆盖布尔等于(obj对象)
{
返回obj是意甲< T> &功放;&安培;这==(意甲< T>)目标文件;
}
公共静态布尔运算符==(意甲< T> S1,意甲< T> S2)
{
返回s1.X == s2.X&放大器;&安培; s1.Y == s2.Y;
}
公共静态布尔运算符=(意甲< T> S1,意甲< T> S2)!
{
回报(S1 == S2);
}

这是行不通的。我在想什么。



 双击[] = XA {2,3}?; 
双[] =雅{1,2};
双[] = XB {2,3};
双[] = YB {1,2};
意甲<双> A =新赛季意甲<双>(XA,YA);
意甲<双> B =新赛季意甲<双>(XB,YB);
Assert.AreEqual(A,B);


解决方案

您要比较的数组的引用的,而不是其内容。 YB 指不同的阵列。如果你想检查数组的内容,你就必须这样做明确



我不的认为的有什么内置进入框架为你做的,我害怕。这样的事情应该工作,虽然:

 公共静态布尔ArraysEqual< T>(T []第一,T []秒)
{
如果(第一==秒)
{
返回真;
}
如果(第一== NULL ||第二== NULL)
{
返回FALSE;
}
如果(first.Length = second.Length!)
{
返回false;
}
的IEqualityComparer比较器= EqualityComparer< T> .DEFAULT;
的for(int i = 0; I< first.Length;我++)
{
如果$ B $(comparer.Equals(第[I],第二[I])!) b {
返回false;
}
}
返回真;
}



顺便说一句,你的结构是那样的可变数组内容可以在创建该结构之后被改变。你真的需要这是一个结构



编辑:尼克在评论中提到的,你真的应该重写GetHashCode为好。再次,你需要从阵列获取的内容(并再次,如果阵列之后得到改变,这将导致问题)。类似的实用方法:

 公共静态INT的GetHashCode< T>(T []数组),
{
如果(阵列== NULL)
{
返回0;
}
的IEqualityComparer比较器= EqualityComparer< T> .DEFAULT;
INT哈希= 17;
的foreach(在t数组项)
{
哈希散列= * 31 + comparer.GetHashCode(项目);
}
返回哈希;
}


I look for an equality between two instances of this struct.

public struct Serie<T>
{
    T[]         X; 
    double[]    Y;

    public Serie(T[] x, double[] y)
    {
        X = x;
        Y = y;
    }

    public override bool Equals(object obj)
    {
        return obj is Serie<T> && this == (Serie<T>)obj;
    } 
    public static bool operator ==(Serie<T> s1, Serie<T> s2)
    {
        return s1.X == s2.X && s1.Y == s2.Y;
    }
    public static bool operator !=(Serie<T> s1, Serie<T> s2)
    {
        return !(s1 == s2);
    }

This doesn't work. What am I missing?

        double[] xa = { 2, 3 };
        double[] ya = { 1, 2 };
        double[] xb = { 2, 3 };
        double[] yb = { 1, 2 };
        Serie<double> A = new Serie<double>(xa, ya);
        Serie<double> B = new Serie<double>(xb, yb);
        Assert.AreEqual(A, B);

解决方案

You're comparing the array references rather than their contents. ya and yb refer to different arrays. If you want to check the contents of the arrays, you'll have to do so explicitly.

I don't think there's anything built into the framework to do that for you, I'm afraid. Something like this should work though:

public static bool ArraysEqual<T>(T[] first, T[] second)
{
    if (first == second)
    {
        return true;
    }
    if (first == null || second == null)
    {
        return false;
    }
    if (first.Length != second.Length)
    {
        return false;
    }
    IEqualityComparer comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < first.Length; i++)
    {
        if (!comparer.Equals(first[i], second[i]))
        {
            return false;
        }
    }
    return true;
}

As an aside, your structs are sort of mutable in that the array contents can be changed after the struct is created. Do you really need this to be a struct?

EDIT: As Nick mentioned in the comments, you should really override GetHashCode as well. Again, you'll need to get the contents from the arrays (and again, this will cause problems if the arrays get changed afterwards). Similar utility method:

public static int GetHashCode<T>(T[] array)
{
    if (array == null)
    {
        return 0;
    }
    IEqualityComparer comparer = EqualityComparer<T>.Default;
    int hash = 17;
    foreach (T item in array)
    {
        hash = hash * 31 + comparer.GetHashCode(item);
    }
    return hash;
}

这篇关于在C#两个结构的平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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