Pair`2.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. namespace PaintDotNet
  3. {
  4. [Serializable]
  5. public struct Pair<T, U>
  6. {
  7. private T first;
  8. private U second;
  9. public T First
  10. {
  11. get
  12. {
  13. return this.first;
  14. }
  15. }
  16. public U Second
  17. {
  18. get
  19. {
  20. return this.second;
  21. }
  22. }
  23. public override int GetHashCode()
  24. {
  25. int firstHash;
  26. int secondHash;
  27. if (object.ReferenceEquals(this.first, null))
  28. {
  29. firstHash = 0;
  30. }
  31. else
  32. {
  33. firstHash = this.first.GetHashCode();
  34. }
  35. if (object.ReferenceEquals(this.second, null))
  36. {
  37. secondHash = 0;
  38. }
  39. else
  40. {
  41. secondHash = this.second.GetHashCode();
  42. }
  43. return firstHash ^ secondHash;
  44. }
  45. public override bool Equals(object obj)
  46. {
  47. return ((obj != null) && (obj is Pair<T, U>) && (this == (Pair<T, U>)obj));
  48. }
  49. public static bool operator ==(Pair<T, U> lhs, Pair<T, U> rhs)
  50. {
  51. bool firstEqual;
  52. bool secondEqual;
  53. if (object.ReferenceEquals(lhs.First, null) && object.ReferenceEquals(rhs.First, null))
  54. {
  55. firstEqual = true;
  56. }
  57. else if (object.ReferenceEquals(lhs.First, null) || object.ReferenceEquals(rhs.First, null))
  58. {
  59. firstEqual = false;
  60. }
  61. else
  62. {
  63. firstEqual = lhs.First.Equals(rhs.First);
  64. }
  65. if (object.ReferenceEquals(lhs.Second, null) && object.ReferenceEquals(rhs.Second, null))
  66. {
  67. secondEqual = true;
  68. }
  69. else if (object.ReferenceEquals(lhs.Second, null) || object.ReferenceEquals(rhs.Second, null))
  70. {
  71. secondEqual = false;
  72. }
  73. else
  74. {
  75. secondEqual = lhs.Second.Equals(rhs.Second);
  76. }
  77. return firstEqual && secondEqual;
  78. }
  79. public static bool operator !=(Pair<T, U> lhs, Pair<T, U> rhs)
  80. {
  81. return !(lhs == rhs);
  82. }
  83. public Pair(T first, U second)
  84. {
  85. this.first = first;
  86. this.second = second;
  87. }
  88. }
  89. }