============================================== Array Custom Comparison
------------------------------------------------------------Person.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text; using system.threading.tasks;using system.collections;namespace consoleapplication4{ public class person:iequatable<person>//inherit IEquatable generic interface { public int Id { get; set; }//attribute ID, no real meaning public string firstname { get; set; } public string lastname { get; set; } public override String tostring () { return string. Format ("Firstname:{0},lastname:{1}", firstname, lastname); &nBsp; } public override bool equals (object obj) { if (obj == null) throw new ArgumentException ("error"); return equals (Obj as person); } public override int gethashcode ()//accompanied by rewrite equals { return id.gethashcode (); } public bool equals (Person other)//Important { &Nbsp; return this. Firstname == other. firstname;//the value of the custom pair (only compared to FirstName) } }}
------------------------------------------------------------Main program
Person p1 = new Person { FirstName = "Caocao", lastname = "Cheng Yufan" }; Person p2 = new person { firstname = "Cao", lastname = "Zhow" }; Person p3 = new person { firstname = "Cao", lastname = "Guan Yu" }; Person p4 = new Person { firstname = "Liu Bei", lastname = "Guan Yu" }; Person[] ps1 = { p1, p2 }; Person[] ps2 = { p1, p3 }; person[] ps3 = { p3, p4 }; // Force an array to be loaded into the IStructuralEquatable interface console.writeline ((ps1 as istructuralequatable). Equals (PS2,&NBSP;EQUALITYCOMPARER<PERSON>. Default);//true console.writeline (PS2 as istructuralequatable). Equals (PS3,&NBSP;EQUALITYCOMPARER<PERSON>. Default);//false console.readkey ();
============================================== Meta-group comparison
var tuple1 = Tuple.create<int, int> (1, 2); var tuple2 = Tuple.create<int, int> (1, 2); Console.WriteLine (Tuple1. Equals (Tuple2));//true Console.WriteLine (tuple1==tuple2);//false Console.readkey ();
This article is from the "Program Ape's Home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1531758