在c#中,从`list

自定义结构体如何影响比较逻辑?
如果结构体实现了`IEquatable

using System;
using System.Collections.Generic;
public struct Point : IEquatable<point>
{
public int X, Y;
public bool Equals(Point other)
{
// 只比较X,忽略Y
return this.X == other.X;
}
public override bool Equals(object obj) => obj is Point p && Equals(p);
public override int GetHashCode() => X.GetHashCode();
}
class Program
{
static void Main()
{
var list = new List<point>
{
new Point { X = 1, Y = 10 },
new Point { X = 1, Y = 20 } // Y不同,但X相同
};
// 尝试移除 X=1, Y=30 的点
// 由于实现了IEquatable,且只比较X,所以会匹配第一个元素
bool removed = list.Remove(new Point { X = 1, Y = 30 });
Console.WriteLine(removed); // True
Console.WriteLine(list.Count); // 1
}
}</point></point>
未实现接口的结构体比较行为是什么?
当结构体未实现`IEquatable

using System;
using System.Collections.Generic;
public struct Person
{
public string Name;
private int age; // 私有字段也会被比较
public Person(string name, int age)
{
this.Name = name;
this.age = age;
}
}
class Program
{
static void Main()
{
var list = new List<person>
{
new Person("Alice", 30)
};
// 创建另一个Name相同但age不同的实例
var target = new Person("Alice", 31);
// 因为age不同,Remove返回False
bool removed = list.Remove(target);
Console.WriteLine(removed); // False
Console.WriteLine(list.Count); // 1
}
}</person>
如何验证特定输入下的删除结果?
为了验证上述行为,可以使用相同的输入数据,分别测试实现和未实现`IEquatable










