Tuple.2.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. public sealed class Tuple<T1, T2>
  3. {
  4. public Tuple(T1 item1, T2 item2)
  5. {
  6. this.item1 = item1;
  7. this.item2 = item2;
  8. }
  9. public T1 Item1
  10. {
  11. get
  12. {
  13. return this.item1;
  14. }
  15. }
  16. public T2 Item2
  17. {
  18. get
  19. {
  20. return this.item2;
  21. }
  22. }
  23. public override string ToString()
  24. {
  25. return string.Format("Tuple({0}, {1})", this.Item1, this.Item2);
  26. }
  27. public override int GetHashCode()
  28. {
  29. int num = 17;
  30. int num2 = num * 23;
  31. int num3;
  32. if (this.item1 == null)
  33. {
  34. num3 = 0;
  35. }
  36. else
  37. {
  38. T1 t = this.item1;
  39. num3 = t.GetHashCode();
  40. }
  41. num = num2 + num3;
  42. int num4 = num * 23;
  43. int num5;
  44. if (this.item2 == null)
  45. {
  46. num5 = 0;
  47. }
  48. else
  49. {
  50. T2 t2 = this.item2;
  51. num5 = t2.GetHashCode();
  52. }
  53. return num4 + num5;
  54. }
  55. public override bool Equals(object o)
  56. {
  57. if (!(o is Tuple<T1, T2>))
  58. {
  59. return false;
  60. }
  61. Tuple<T1, T2> b = (Tuple<T1, T2>)o;
  62. return this == b;
  63. }
  64. public bool Equals(Tuple<T1, T2> other)
  65. {
  66. return this == other;
  67. }
  68. public static bool operator ==(Tuple<T1, T2> a, Tuple<T1, T2> b)
  69. {
  70. if (object.ReferenceEquals(a, null))
  71. {
  72. return object.ReferenceEquals(b, null);
  73. }
  74. if (a.item1 == null && b.item1 != null)
  75. {
  76. return false;
  77. }
  78. if (a.item2 == null && b.item2 != null)
  79. {
  80. return false;
  81. }
  82. T1 t = a.item1;
  83. bool result;
  84. if (t.Equals(b.item1))
  85. {
  86. T2 t2 = a.item2;
  87. result = t2.Equals(b.item2);
  88. }
  89. else
  90. {
  91. result = false;
  92. }
  93. return result;
  94. }
  95. public static bool operator !=(Tuple<T1, T2> a, Tuple<T1, T2> b)
  96. {
  97. return !(a == b);
  98. }
  99. public void Unpack(Action<T1, T2> unpackerDelegate)
  100. {
  101. unpackerDelegate(this.Item1, this.Item2);
  102. }
  103. private readonly T1 item1;
  104. private readonly T2 item2;
  105. }