Guest User

Untitled

a guest
Jan 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ArrayEqualityComparer<TElement> : IEqualityComparer<TElement[]>
  5. {
  6. public static readonly ArrayEqualityComparer<TElement> Default =
  7. new ArrayEqualityComparer<TElement>(EqualityComparer<TElement>.Default);
  8.  
  9. private readonly IEqualityComparer<TElement> elementEqualityComparer;
  10.  
  11. public ArrayEqualityComparer(IEqualityComparer<TElement> elementEqualityComparer) =>
  12. this.elementEqualityComparer = elementEqualityComparer ?? throw new ArgumentNullException(nameof(elementEqualityComparer));
  13.  
  14. public Boolean Equals(TElement[] x, TElement[] y)
  15. {
  16. if (ReferenceEquals(x, y)) // both null or both referencing the same array object
  17. return true;
  18. if (x is null | y is null)
  19. return false;
  20. if (x.Length != y.Length)
  21. return false;
  22. for (var i = 0; i != x.Length; i++)
  23. if (!elementEqualityComparer.Equals(x[i], y[i]))
  24. return false;
  25. return true;
  26. }
  27.  
  28. public Int32 GetHashCode(TElement[] types)
  29. {
  30. var hashCode = 0x51ed270b;
  31. if (types == null)
  32. return hashCode;
  33. for (var i = 0; i != types.Length; i++)
  34. hashCode = hashCode * -1521134295 + elementEqualityComparer.GetHashCode(types[i]);
  35. return hashCode;
  36. }
  37. }
Add Comment
Please, Sign In to add comment