可以检查两个列表是否相等吗?

huangapple go评论79阅读模式
英文:

Is it possible to check if two lists are equal?

问题

我正在使用两个列表。它们都具有完全相同的属性和命名。
是否可能检查这两个列表是否相等?

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        this.Name = name;
        this.Age = age;
    }
}

var list1 = new List<Person>();
list1.Add(new Person("Dim", 12));
list1.Add(new Person("Dio", 13));

var list2 = new List<Person>();
list2.Add(new Person("Dim", 12));
list2.Add(new Person("Dio", 13));

bool listsEqual = list1 == list2;

Debug.WriteLine("Lists are equal: " + listsEqual);
英文:

I am using two lists. Both of them have exactly the same properties and namings.
Is it possible to check the both lists are equal or not?

public class Person
 {
     public string Name { get; set; }
     public int Age { get; set; }
     public Person(string name, int age)
     {
         this.Name = name;
         this.Age = age;
     }
 }


 var list1 = new List&lt;Person&gt;();
 list1.Add(new Person(&quot;Dim&quot;, 12));
 list1.Add(new Person(&quot;Dio&quot;, 13));

 var list2 = new List&lt;Person&gt;();
 list2.Add(new Person(&quot;Dim&quot;, 12));
 list2.Add(new Person(&quot;Dio&quot;, 13));

 bool listsEqual = list1 == list2;

 Debug.WriteLine(&quot;Lists are equal: &quot; + listsEqual);

答案1

得分: 3

你需要使用IEquatable接口来实现这个功能。

public class Person : IEquatable<Person>
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        this.Name = name;
        this.Age = age;
    }

    public bool Equals(Person other)
    {
        if (other is null)
            return false;

        return this.Name == other.Name && this.Age == other.Age;
    }

    public override bool Equals(object obj) => Equals(obj as Person);
    public override int GetHashCode() => (Name, Age).GetHashCode();
}

然后你可以使用System.Linq中的SequenceEqual()方法:

var list1 = new List<Person>();
list1.Add(new Person("Dim", 12));
list1.Add(new Person("Dio", 13));

var list2 = new List<Person>();
list2.Add(new Person("Dim", 12));
list2.Add(new Person("Dio", 13));

bool test = list1.SequenceEqual(list2);
Console.WriteLine(test);
英文:

You need to use IEquatable Interface for this.

public class Person : IEquatable&lt;Person&gt;
 {
     public string Name { get; set; }
     public int Age { get; set; }
     public Person(string name, int age)
     {
         this.Name = name;
         this.Age = age;
     }
	public bool Equals(Person other)
    {
        if (other is null)
            return false;

        return this.Name == other.Name &amp;&amp; this.Age == other.Age;
    }

    public override bool Equals(object obj) =&gt; Equals(obj as Person);
    public override int GetHashCode() =&gt; (Name, Age).GetHashCode();
 }

Then you can use SequenceEqual() method from System.Linq

var list1 = new List&lt;Person&gt;();
		 list1.Add(new Person(&quot;Dim&quot;, 12));
		 list1.Add(new Person(&quot;Dio&quot;, 13));

		 var list2 = new List&lt;Person&gt;();
		 list2.Add(new Person(&quot;Dim&quot;, 12));
		 list2.Add(new Person(&quot;Dio&quot;, 13));
 
		bool test =	list1.SequenceEqual(list2);
		Console.WriteLine(test);

答案2

得分: 1

在C#中,当你使用==运算符来比较两个列表时,它将检查这两个列表是否引用了内存中相同的对象,而不是它们的内容是否相同。

可以使用System.Linq命名空间中的SequenceEqual方法。

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-7.0

英文:

In C#, when you use the == operator to compare two lists, it will check if the two lists reference the same object in memory, not if their contents are the same.

use the SequenceEqual method from the System.Linq namespace.

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-7.0

huangapple
  • 本文由 发表于 2023年8月10日 15:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定