Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to compare arrays and tuples in the new features of .NET 4

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to compare arrays and tuples in the new features of .NET 4? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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// inherits the IEquatable generic interface {public int Id {get; set;} / / attribute id, which has no practical significance public string FirstName {get; set;} public string LastName {get; set } public override string ToString () {return string.Format ("FirstName: {0}, LastName: {1}", FirstName, LastName);} public override bool Equals (object obj) {if (obj = = null) throw new ArgumentException ("error"); return Equals (obj as Person) } public override int GetHashCode () / / accompanied by the rewritten Equals {return Id.GetHashCode ();} public bool Equals (Person other) / / important {return this.FirstName = = other.FirstName;// customized values to be compared (only for FirstName)}

Main program

Person p1 = new Person {FirstName = "Cao Cao", LastName = "Cheng Yu"}; Person p2 = new Person {FirstName = "Cao Cao", LastName = "Xunyou"}; Person p3 = new Person {FirstName = "Cao 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}; / / forcibly replace the array with IStructuralEquatable interface Console.WriteLine ((ps1 as IStructuralEquatable) .equals (ps2, EqualityComparer.Default)); / / True Console.WriteLine ((ps2 as IStructuralEquatable) .equals (ps3, EqualityComparer.Default)); / / False Console.ReadKey ()

Tuple comparison

Var tuple1= Tuple.Create (1,2); var tuple2 = Tuple.Create (1,2); Console.WriteLine (tuple1.Equals (tuple2)); / / True Console.WriteLine (tuple1==tuple2); / / False Console.ReadKey () This is the answer to the question on how to compare arrays and tuples in the new features of .NET 4. I hope the above content can be of some help to you, if you still have a lot of doubts to be solved. You can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report