英文:
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<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);
答案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<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();
}
Then you can use SequenceEqual()
method from System.Linq
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);
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论