How to use the list collection operation IEqualityComparer in C # correctly

Source: Internet
Author: User

C # in the list collection operation IEqualityComparer Correct use method except, Intersect, union method IEqualityComparer how to use

For normal string, and other basic types do not need to be implemented, but when your own reference type entity needs to be compared, it needs to be implemented, especially the more complicated

IEqualityComparer How to achieve

Requires implementations bool Equals(CNMemo x, CNMemo y) and int GetHashCode(CNMemo obj) methods, and inherits IEqualityComparer

ReSharper plug-ins can be implemented by default with Alt-insert shortcut key insertion

I have four properties to compare

Memoid:string

Content:string

Title:string

Tags:list

  private sealed class cnmemoequalitycomparer:iequalitycomparer<cnmemo>{public bool Equal        S (Cnmemo x, Cnmemo y) {if (ReferenceEquals (x, y)) return true;        if (referenceequals (x, NULL)) return false;        if (referenceequals (y, NULL)) return false;        if (x.gettype () = Y.gettype ()) return false; return string. Equals (x.memoid, y.memoid) && string. Equals (X.content, y.content) && string.    Equals (X.title, Y.title) && ((x.tags = = NULL && y.tags = = x.tags) | | (X.tags! = NULL && y.tags! = null && X.tags.count = = Y.tags.count &&!x.tags.except (y.tags).    Any ())); } public int GetHashCode (Cnmemo obj) {return obj. ToString ().    GetHashCode (); }}public static iequalitycomparer<cnmemo> cnmemocomparer {get;} = new Cnmemoequalitycomparer ();  

Note: The GetHashCode method is executed first in this method, and if the GetHashCode method returns a different value, it ignores the Equals method directly, So I didn't use the default implementation of ReSharper in GetHashCode

public int GetHashCode(CNMemo obj){    unchecked    {        var hashCode = (obj.MemoId != null ? obj.MemoId.GetHashCode() : 0);        hashCode = (hashCode * 397) ^ (obj.Content != null ? obj.Content.GetHashCode() : 0);        hashCode = (hashCode * 397) ^ (obj.Title != null ? obj.Title.GetHashCode() : 0);        hashCode = (hashCode * 397) ^ (obj.Tags != null ? obj.Tags.GetHashCode() : 0);        return hashCode;    }}

Because this is not the same as the tags reference address will cause GetHashCode inconsistencies, obj.ToString().GetHashCode() if the ToString method returns the same content, then the comparison will be used bool Equals(CNMemo x, CNMemo y)

If you have implemented it before IEquatable<CNMemo> , you can use it x.Equals(y) to complete the comparison so that the comparison code does not have to copy two copies.

That's it when you use it.new List<CNMemo>() { memo1 }.Except(new List<CNMemo>() { memo2 }, CNMemo.CnMemoComparer)

IEquatable

If you implement IEquatable<CNMemo> and pay attention to the implementation of the GetHashCode method, pay attention to the above problem, but also directly usenew List<CNMemo>() { memo1 }.Except(new List<CNMemo>() { memo2 })

However, the default GetHashCode such implementation has any negative effect to follow-up research, no side effects found at present

How to use the list collection operation IEqualityComparer in C # correctly

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.