111,126
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
TestClass tc1 = new TestClass();
tc1.i = 1;
TestClass tc2 = new TestClass();
tc2.i = 1;
List<TestClass> list = new List<TestClass>();
list.Add(tc1);
Console.WriteLine(list.Contains(tc2));
Console.WriteLine(list.Contains(tc2, new TestComparer<TestClass>()));
}
class TestClass
{
public int i;
}
class TestComparer<T> : IEqualityComparer<T>
where T : TestClass
{
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
public bool Equals(T t1, T t2)
{
return t1.i == t2.i;
}
}