using System;
using System.Collections.Generic;
internal class GenericEqualityComparer<T> : EqualityComparer<T> where T : IEquatable<T>
{
public override bool Equals(T x, T y)
{
if (x != null)
{
if (y != null) return x.Equals(y);
return false;
}
if (y != null) return false;
return true;
}
public override int GetHashCode(T obj) => obj?.GetHashCode() ?? 0;
// ...
}
internal struct Foo : IEquatable<Foo>
{
public bool Equals(Foo other)
{
Console.WriteLine("Equals(Foo other)");
return true;
}
public override bool Equals(object obj)
{
Console.WriteLine("Equals(object obj)");
return true;
}
public override int GetHashCode() => 0;
}
internal static class Program
{
private static void Main()
{
var comparer = new GenericEqualityComparer<Foo>();
var x = new Foo();
var y = new Foo();
var equal = comparer.Equals(x, y);
Console.WriteLine(equal);
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKCmludGVybmFsIGNsYXNzIEdlbmVyaWNFcXVhbGl0eUNvbXBhcmVyPFQ+IDogRXF1YWxpdHlDb21wYXJlcjxUPiB3aGVyZSBUIDogSUVxdWF0YWJsZTxUPgp7CiAgICBwdWJsaWMgb3ZlcnJpZGUgYm9vbCBFcXVhbHMoVCB4LCBUIHkpCiAgICB7CiAgICAgICAgaWYgKHggIT0gbnVsbCkKICAgICAgICB7CiAgICAgICAgICAgIGlmICh5ICE9IG51bGwpIHJldHVybiB4LkVxdWFscyh5KTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBpZiAoeSAhPSBudWxsKSByZXR1cm4gZmFsc2U7CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CgogICAgcHVibGljIG92ZXJyaWRlIGludCBHZXRIYXNoQ29kZShUIG9iaikgPT4gb2JqPy5HZXRIYXNoQ29kZSgpID8/IDA7CgogICAgLy8gLi4uCn0KCmludGVybmFsIHN0cnVjdCBGb28gOiBJRXF1YXRhYmxlPEZvbz4KewogICAgcHVibGljIGJvb2wgRXF1YWxzKEZvbyBvdGhlcikKICAgIHsKICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiRXF1YWxzKEZvbyBvdGhlcikiKTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KCiAgICBwdWJsaWMgb3ZlcnJpZGUgYm9vbCBFcXVhbHMob2JqZWN0IG9iaikKICAgIHsKICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiRXF1YWxzKG9iamVjdCBvYmopIik7CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9CgogICAgcHVibGljIG92ZXJyaWRlIGludCBHZXRIYXNoQ29kZSgpID0+IDA7Cn0KCmludGVybmFsIHN0YXRpYyBjbGFzcyBQcm9ncmFtCnsKICAgIHByaXZhdGUgc3RhdGljIHZvaWQgTWFpbigpCiAgICB7CiAgICAgICAgdmFyIGNvbXBhcmVyID0gbmV3IEdlbmVyaWNFcXVhbGl0eUNvbXBhcmVyPEZvbz4oKTsKICAgICAgICB2YXIgeCA9IG5ldyBGb28oKTsKICAgICAgICB2YXIgeSA9IG5ldyBGb28oKTsKICAgICAgICB2YXIgZXF1YWwgPSBjb21wYXJlci5FcXVhbHMoeCwgeSk7CiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoZXF1YWwpOwogICAgfQp9Cg==