Tango
  • Introduction
  • Getting Started
    • Summary
    • From me, the Developer
    • What is Tango?
    • Where to start?
  • Installation
    • Based on NuGet
    • Manually
  • Fundamentals
    • Introduction
    • Using Pattern Matching
    • Option values
    • Either values
    • From void to Unit
    • Func and Action
    • Chainable operations in a Continuation flow
    • Currying and Partial Application
  • Functional
    • Introduction
    • Currying
    • Partial Application
    • Functional Extensions
    • QuickCast to Delegates
  • Operations
    • Introduction
    • Operations with Booleans
    • Operations with Integers
    • Operations with Decimals
    • Operations with Doubles
    • Operations with Strings
  • Types
    • Introduction
    • Unit
    • Option<T>
    • Either<TLeft, TRight>
    • Continuation<TFail, TSuccess>
  • Modules
    • Introduction
    • Option
      • Apply
      • AsEnumerable
      • Bind
      • Count
      • Exists
      • Filter
      • Fold
      • FoldBack
      • Iterate
      • Map
      • OfNullable
      • ToArray
      • ToList
      • ToNullable
    • Either
      • Exists
      • Iterate
      • Fold
      • FoldBack
      • Map
      • Swap
      • ToTuple
    • Continuation
      • AsContinuation
      • Resolve
      • Reject
      • All
    • Collection
      • Append
      • Choose
      • ChunkBySize
      • Collect
      • CompareWith
      • CountBy
      • Concat
      • Distinct
      • Empty
      • Exists
      • Exists2
      • Filter
      • FindIndex
      • Fold
      • Fold2
      • FoldBack
      • FoldBack2
      • ForAll
      • ForAll2
      • ForAll3
      • Head
      • HeadAndTailEnd
      • Range
      • Generate
      • Initialize
      • Iterate
      • Iterate2
      • IterateIndexed
      • IterateIndexed2
      • Map
      • Map2
      • Map3
      • MapIndexed
      • MapIndexed2
      • MapIndexed3
      • Partition
      • Permute
      • Pick
      • Reduce
      • ReduceBack
      • Replicate
      • Scan
      • Scan2
      • ScanBack
      • ScanBack2
      • Tail
      • TryFind
      • TryPick
      • Unzip
      • Unzip3
      • Zip
      • Zip3
  • Extensions
    • Introduction
    • Enum Extensions
    • EqualityComparer Builder
    • Modules as Extensions
Powered by GitBook
On this page
  • Properties
  • Methods
  • Usage

Was this helpful?

  1. Extensions

EqualityComparer Builder

PreviousEnum ExtensionsNextModules as Extensions

Last updated 5 years ago

Was this helpful?

This class implements interface and provides a method that can be used to create concrete objects that implements this comparison interface.

You can use this builder to define a dynamic comparison between two objects, you can inform this comparer as parameter for method from namespace.

It's a better option than implements interface directly in class that you want to compare, because you can define multiple ways to compare same object types.

Properties

Name

Type

Description

Comparer

Func<T, T, bool>

Method used by Equals interface method.

HashCodeGetter

Func<T, int>

Method used by GetHashCode interface method.

Methods

Name

Parameter

Returns

Description

Create

Func<T, T, bool> comparer

Func<T, int> hashCodeGetter

EqualityComparerBuilder<T>

Creates a new object that implements IEqualityComparer<T>.

Equals

T x

T y

bool

IEqualityComparer<T> interface method.

GetHashCode

T obj

int

IEqualityComparer<T> interface method.

Usage

You can use the static method Create to creates a new instance of a comparer object.

See the code:

class Product
{
   public int Id { get; set; }
   public string Description { get; set; }
}

Func<Product, Product, bool> compareProducts = 
      (product1, product2) => product1.Id == product2.Id;

Func<Product, int> getHashCodeProduct =
      product => product.Id.GetHashCode();

var comparer = 
    EqualityComparerBuilder<Product>.Create(
        compareProducts, 
        getHashCodeProduct);

IEnumerable<Product> products = GetProducts();
products.Distinct(comparer);

In the previous example, the Distinct method will no longer comparer the object referece. The comparison will be made by comparisons of comparer object.

You can use anonymous methods directly as Create method parameters:

class Product
{
   public int Id { get; set; }
   public string Description { get; set; }
}
IEnumerable<Product> products = GetProducts();
products.Distinct(
    EqualityComparerBuilder<Product>.Create(
            (product1, product2) => product1.Id == product2.Id, 
            product => product.Id.GetHashCode());
);
Tango.Linq.EqualityComparerBuilder<T>
IEqualityComparer<T>
Distinct
System.Linq