C#控制台应用程序中的SequenceEqual无法正常工作

时间:2019-02-04 17:35:34

标签: c# comparison

我正在使用c#.NET 4.7.2编写控制台应用程序。我有2个对象列表,我试图查看它们是否相等。我正在使用SequenceEqual来执行此操作,但是当它应该返回true时它返回false。

这是我的代码:

List<Objects.PackageDetails> ExpectedResult1 = new List<Objects.PackageDetails>()
            {
                new Objects.PackageDetails{PackageName = "KittenService", PackageDependency = "CamelCaser"},
                new Objects.PackageDetails{PackageName = "CamelCaser", PackageDependency = ""}
            };

            List<Objects.PackageDetails> ExpectedResult = new List<Objects.PackageDetails>()
            {
                new Objects.PackageDetails{PackageName = "KittenService", PackageDependency = "CamelCaser"},
                new Objects.PackageDetails{PackageName = "CamelCaser", PackageDependency = ""}
            };

            var a = ExpectedResult1.SequenceEqual(ExpectedResult);

            Console.WriteLine(">" + ExpectedResult1[0].PackageName + "<");
            Console.WriteLine(">" + ExpectedResult[0].PackageName + "<");
            Console.WriteLine(">" + ExpectedResult1[0].PackageDependency + "<");
            Console.WriteLine(">" + ExpectedResult[0].PackageDependency + "<");

            Console.WriteLine(">" + ExpectedResult1[1].PackageName + "<");
            Console.WriteLine(">" + ExpectedResult[1].PackageName + "<");
            Console.WriteLine(">" + ExpectedResult1[1].PackageDependency + "<");
            Console.WriteLine(">" + ExpectedResult[1].PackageDependency + "<");

            if (ExpectedResult1[0].PackageName == ExpectedResult[0].PackageName)
            {
                Console.WriteLine("equal");
            }
            else
            {
                Console.WriteLine("not equal");
            }

            if (ExpectedResult1[1].PackageName == ExpectedResult[1].PackageName)
            {
                Console.WriteLine("equal");
            }
            else
            {
                Console.WriteLine("not equal");
            }

            if (ExpectedResult1[0].PackageDependency == ExpectedResult[0].PackageDependency)
            {
                Console.WriteLine("equal");
            }
            else
            {
                Console.WriteLine("not equal");
            }

            if (ExpectedResult1[1].PackageDependency == ExpectedResult[1].PackageDependency)
            {
                Console.WriteLine("equal");
            }
            else
            {
                Console.WriteLine("not equal");
            }
Console.Readline();

我正在将两个列表相互比较,当我将鼠标悬停在var a上时,它表示这是错误的。但是所有显示语句显然都显示出相同的值,并且比较各个值的if语句也表明它们也相同。

有人会给我一个想法,为什么SequenceEqual说两个列表不相等,但是我所做的确定它们是否相等的其他一切都说它们相等?预先感谢。

编辑:这是我的类声明,以防万一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Objects
{
    public class PackageDetails
    {
        public string PackageName { get; set; }
        public string PackageDependency { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

平等不是“看起来一样”;对于对象,相等性默认值是相同的引用,在这种情况下,它们将不是(因为每个列表分别创建了单独的对象,通过new Objects.PackageDetails)。如果您想自定义相等性,则需要覆盖Equals(理想情况下还应为IEquatable<T>实现T相同类型),并在您执行那个您还需要正确实现GetHashCode()。因此:PackageDetails是否定义了自定义相等性?如果没有:这完全符合我的期望。

一种偷偷摸摸但有效的代码实现方法是滥用价值元组:

public override bool Equals(object obj) => Equals(obj as PackageDetails);
public bool Equals(PackageDetails obj) {
    if(obj == null) return false;
    return (PackageName, PackageDependency).Equals((obj.PackageName, obj.PackageDependency));
}
public override int GetHashCode() {
   return (PackageName, PackageDependency).GetHashCode();
}