Skip to content

Instantly share code, notes, and snippets.

@qubitz
Last active August 11, 2021 02:12
Show Gist options
  • Save qubitz/90e96027ed36c5ff6a87182c3561b511 to your computer and use it in GitHub Desktop.
Save qubitz/90e96027ed36c5ff6a87182c3561b511 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
var data1 = new Data("data1");
var data2 = new Data("data2");
Console.WriteLine(new DataEqualityComparer().Equals(data1, data2));
public record Data(string SomeProp);
public class DataEqualityComparer : IEqualityComparer<Data>
{
public bool Equals(Data x, Data y)
{
return
ReferenceEquals(x, y) || // true when: both null, same ref
x is null == y is null && // true when: both not null
x.SomeProp == y.SomeProp; // true when: equal properties
}
/// <inheritdoc />
public int GetHashCode(
Data obj)
{
return HashCode.Combine(obj.SomeProp);
}
}
{
"version": 1,
"target": "Run",
"mode": "Debug",
"branch": "core-x64"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment