C # Collection Implementation of the de-weight, intersection, difference set, etc., used to compare, there are several ways to meet.
Data:
varList =NewList<myclass> { NewMyClass {MyProperty1 ="Ten", MyProperty2 =" -" }, NewMyClass {MyProperty1 ="Ten", MyProperty2 =" -" }, NewMyClass {MyProperty1 =" -", MyProperty2 =" -" }};
Method One:
Public classmyclass{ Public stringMyProperty1 {Get;Set; } Public stringMyProperty2 {Get;Set; } Public Override BOOLEquals (Objectobj) { if(ReferenceEquals (obj,NULL))return false; if(ReferenceEquals ( This, obj))return true; varo = obj asMyClass; returno.myproperty1.equals (o.myproperty1); } Public Override intGetHashCode () {returnMyproperty1.gethashcode (); }}
Usage:
List. Distinct (); // Go heavy
Method Two:
Public classMyclass:iequatable<myclass>{ Public stringMyProperty1 {Get;Set; } Public stringMyProperty2 {Get;Set; } Public BOOLEquals (MyClass other) {if(ReferenceEquals (Other,NULL))return false; if(ReferenceEquals ( This, other))return true; returnMyproperty1.equals (Other. MYPROPERTY1); }}
Usage:
List. Distinct (); // Go heavy
Method Three:
Public classmyclass{ Public stringMyProperty1 {Get;Set; } Public stringMyProperty2 {Get;Set; }} Public classMyclassiequalitycomparer:iequalitycomparer<myclass>{ Public BOOLEquals (MyClass x, MyClass y) {if(ReferenceEquals (x,NULL))return false; if(ReferenceEquals (Y,NULL))return false; if(ReferenceEquals (x, y))return true; returnx.myproperty1.equals (y.myproperty1); } Public intGetHashCode (MyClass obj) {returnobj. Myproperty1.gethashcode (); }}
Usage:
List. Distinct (new myclassiequalitycomparer ()); // Go heavy
C # Multiple Comparator implementations