C # Programming (37)----------structure comparison

Source: Internet
Author: User

Structure comparison

Both arrays and tuples implement interfaces istructuralequatable and istructuralcomparable. Not only can the two interfaces compare references , but they can also compare content . These interfaces are display implementations , so you need to cast the arrays and tuples to this interface when you use them . The IStructuralEquatable interface is used to compare whether two tuples or arrays have the same content , and the IStructuralComparable interface is used to sort tuples or arrays .

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

Namespace structure Comparison

{

Class Program

{

static void Main (string[] args)

{

creates an array of two person items .

comparison operator! = returns true

because this is actually the two different arrays referenced by the Two variables p1 and p2.

because the array class does not override Equals () with one parameter, the "= =" operator

The comparison reference will get the same structure, that is , the two variables are not the same

Person Zhangsan = new Person {FirstName = "Zhang", LastName = "san"};

Person[] P1 = {

New person

{

Firstname= "Lisi"

} ,

Zhangsan

};

Person[] P2 = {

New person

{

Firstname= "Lisi"

} ,

Zhangsan

};

if (P1!=P2)

{

Console.WriteLine ("Not the same reference");

}

Console.readkey ();

}

}

public class Person:iequatable<person>

{

public int ID {get; set;}

public string FirstName {set; get;}

public string LastName {set; get;}

public override string ToString ()

{

return string. Format ("{0},{1}{2}", ID, FirstName, LastName);;

}

public override bool Equals (object obj)

{

if (obj = = null)

{

throw new ArgumentNullException ("obj");

}

Return Equals (obj as person);

}

public override int GetHashCode ()

{

Return ID. GetHashCode ();

}

public bool Equals (person other)

{

if (other = = null)

{

throw new ArgumentNullException ("other");

}

return this.id = = Other.id && this. FirstName = = Other. FirstName && this. LastName = = Other. LastName;

}

}

}

Use the person class that implements the IEquatable interface . The IEquatable interface defines a strongly typed Equals () method that is used to compare The values of the FirstName and LastName properties .

Here's a look at how to do the same for tuples . There are two identical tuple instances created :

var T1 = tuple.create<string, int> ("Zhangsan", 19);

var t2 = tuple.create<string, int> ("Zhangsan", 19);

because t1 and T2 refer to two different objects , the comparison operator "! =" returns true

if (t1! = t2)

{

Console.WriteLine ("Not the same reference to the tuple");

}

This calls the Object.Equals () method to compare each item of the tuple , each of which returns true

if (t1. Equals (T2))

{

Console.WriteLine ("The same reference to the tuple");

}

Tuple<>class provides a total of twoEquals ()Method: a rewrite object equals () method ,object as parameter , second by Interface definition ,objectiequalitycomparer as parameter

You can also use class Tuplecomparer to create a custom uequalitycomparer that implements the two methods of the IEqualityComparer interface Equals ( ) and GetHashCode ():

public class Tuplecomparer:iequalitycomparer

{

public bool Equals (object x, Object y)

{

return x.equals (y);

}

public int GetHashCode (object obj)

{

return obj. GetHashCode ();

}

}

The Equals () method that implements the IEqualityComparer interface requires a new modifier or an implicitly implemented interface because the base class object A static Equals () method with two parameters is also defined.

UseTuplecomparer,To giveTuple<t1,t2>Class ofEquals () method to pass a new instance equals (0 method for each call to compare tuplecomparerequals () method so , for Span style= "Font-family:times New Roman" >tuple<t1,t2>, to invoke two tuplecomparer,< Span style= "font-family: Arial" To check that all items are equal

C # Programming (37)----------structure comparison

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.