Pair`2.cs 2.5 KB

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