fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. internal class GenericEqualityComparer<T> : EqualityComparer<T> where T : IEquatable<T>
  5. {
  6. public override bool Equals(T x, T y)
  7. {
  8. if (x != null)
  9. {
  10. if (y != null) return x.Equals(y);
  11. return false;
  12. }
  13. if (y != null) return false;
  14. return true;
  15. }
  16.  
  17. public override int GetHashCode(T obj) => obj?.GetHashCode() ?? 0;
  18.  
  19. // ...
  20. }
  21.  
  22. internal struct Foo : IEquatable<Foo>
  23. {
  24. public bool Equals(Foo other)
  25. {
  26. Console.WriteLine("Equals(Foo other)");
  27. return true;
  28. }
  29.  
  30. public override bool Equals(object obj)
  31. {
  32. Console.WriteLine("Equals(object obj)");
  33. return true;
  34. }
  35.  
  36. public override int GetHashCode() => 0;
  37. }
  38.  
  39. internal static class Program
  40. {
  41. private static void Main()
  42. {
  43. var comparer = new GenericEqualityComparer<Foo>();
  44. var x = new Foo();
  45. var y = new Foo();
  46. var equal = comparer.Equals(x, y);
  47. Console.WriteLine(equal);
  48. }
  49. }
  50.  
Success #stdin #stdout 0.02s 29800KB
stdin
Standard input is empty
stdout
Equals(Foo other)
True