private void test() {
List<ITT > ts = new List<ITT >();
ts.Add(new TT("rongs",10,"zhutou"));
ts.Add(new TT("rongrong", 10, "zhutou"));
ts.Add(new TT("rr", 10, "zhutou"));
ts.Add(new TT("wgr", 10, "zhutou"));
TT t = new TT("wgr", 10, "zhutou");
bool bc = ts.Contains (t);
Console.WriteLine(bc); //true
}
// 扩展该函数可对两个集合进行细致比较
private List<ITT > FindDif(List<ITT > ts1, List<ITT > ts2) {
List<ITT > difT = new List<ITT >();
if (ts1== null || ts1.Count == 0) {
return ts2;
}
foreach (ITT t in ts2) {
if (!ts1.Contains(t )) {
difT.Add(t );
}
}
return difT;
}
public interface ITT : IEquatable <ITT > {
string Name { get; set; }
string Address { get; set; }
int Age { get; set; }
}
public class TT : ITT {
public TT(string name, int age, string address) {
this.name = name;
this.age = age;
this.address = address;
}
private string name;
public string Name {
get { return name; }
set { name = value; }
}
private int age;
public int Age {
get { return age; }
set { age = value; }
}
private string address;
public string Address {
get { return address; }
set { address = value; }
}
#region IEquatable<TT> Members
public bool Equals(ITT other) {
return name.Equals(other.Name) && age.Equals(other.Age) && address.Equals(other.Address);
}
#endregion
}
MSDN参考资料
List< (Of < ( T > ) > ) . . :: . Contains Method
This method determines equality using the default equality comparer EqualityComparer< (Of < ( T> ) > ) . . :: . Default for T , the type of values in the list.
EqualityComparer< (Of < ( T> ) > ) . . :: . Default
The Default property checks whether type T implements the System. . :: . IEquatable< (Of < ( T> ) > ) generic interface and if so returns an EqualityComparer< (Of < ( T> ) > ) that uses that implementation. Otherwise it returns an EqualityComparer< (Of < ( T> ) > ) that uses the overrides of Object. . :: . Equals and Object. . :: . GetHashCode provided by T .