. NET 4 new features-arrays, tuple comparisons

Source: Internet
Author: User

============================================== 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&LT;PERSON&GT;. Default);//true            console.writeline (PS2  as istructuralequatable). Equals (PS3,&NBSP;EQUALITYCOMPARER&LT;PERSON&GT;. 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

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.