C # comparison of two complex object sets

Source: Internet
Author: User

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
}

 

// Extend this function to compare the two sets in detail

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 references

List <
(
<
(
T
>
)
>
)
.
.
::
.
Contains
Method

This method determines equality using the default equality comparer equalitycomparer <
(
<
(
T>
)
>
)
.
.
::
.
Default
For
T
, The type of values in the list.

 

Equalitycomparer <
(
<
(
T>
)
>
)
.
.
::
.
Default

The default
Property checks whether type T
Implements the system.
.
::
.
Iequatable <
(

<
(
T>
)
>
)

Generic interface and if so returns an equalitycomparer <
(
<
(
T>
)
>
)

That uses that implementation.
Otherwise it returns an equalitycomparer <
(
<
(
T>
)
>
)

That uses the overrides of object.
.
::
.
Equals
And object.
.
::
.
Gethashcode
Provided by T
.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.