Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 11:13:22 10月03日 Update

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.

Tags: Array question feature interface more Guan Yu help solution easy important nothing easy main program content actual buddy partner property meaning method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi MariaDB Shulou Technology OPPO Reno