ValueTuple.cs 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. #pragma warning disable SA1141 // explicitly not using tuple syntax in tuple implementation
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Runtime.InteropServices;
  9. using System.Runtime.CompilerServices;
  10. namespace System
  11. {
  12. /// <summary>
  13. /// Helper so we can call some tuple methods recursively without knowing the underlying types.
  14. /// </summary>
  15. internal interface IValueTupleInternal : ITuple
  16. {
  17. int GetHashCode(IEqualityComparer comparer);
  18. string ToStringEnd();
  19. }
  20. /// <summary>
  21. /// The ValueTuple types (from arity 0 to 8) comprise the runtime implementation that underlies tuples in C# and struct tuples in F#.
  22. /// Aside from created via language syntax, they are most easily created via the ValueTuple.Create factory methods.
  23. /// The System.ValueTuple types differ from the System.Tuple types in that:
  24. /// - they are structs rather than classes,
  25. /// - they are mutable rather than readonly, and
  26. /// - their members (such as Item1, Item2, etc) are fields rather than properties.
  27. /// </summary>
  28. [Serializable]
  29. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  30. public struct ValueTuple
  31. : IEquatable<ValueTuple>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple>, IValueTupleInternal, ITuple
  32. {
  33. /// <summary>
  34. /// Returns a value that indicates whether the current <see cref="ValueTuple"/> instance is equal to a specified object.
  35. /// </summary>
  36. /// <param name="obj">The object to compare with this instance.</param>
  37. /// <returns><see langword="true"/> if <paramref name="obj"/> is a <see cref="ValueTuple"/>.</returns>
  38. public override bool Equals(object? obj)
  39. {
  40. return obj is ValueTuple;
  41. }
  42. /// <summary>Returns a value indicating whether this instance is equal to a specified value.</summary>
  43. /// <param name="other">An instance to compare to this instance.</param>
  44. /// <returns>true if <paramref name="other"/> has the same value as this instance; otherwise, false.</returns>
  45. public bool Equals(ValueTuple other)
  46. {
  47. return true;
  48. }
  49. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  50. {
  51. return other is ValueTuple;
  52. }
  53. int IComparable.CompareTo(object? other)
  54. {
  55. if (other == null) return 1;
  56. if (!(other is ValueTuple))
  57. {
  58. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  59. }
  60. return 0;
  61. }
  62. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  63. /// <param name="other">An instance to compare.</param>
  64. /// <returns>
  65. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  66. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  67. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  68. /// than <paramref name="other"/>.
  69. /// </returns>
  70. public int CompareTo(ValueTuple other)
  71. {
  72. return 0;
  73. }
  74. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  75. {
  76. if (other == null) return 1;
  77. if (!(other is ValueTuple))
  78. {
  79. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  80. }
  81. return 0;
  82. }
  83. /// <summary>Returns the hash code for this instance.</summary>
  84. /// <returns>A 32-bit signed integer hash code.</returns>
  85. public override int GetHashCode()
  86. {
  87. return 0;
  88. }
  89. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  90. {
  91. return 0;
  92. }
  93. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  94. {
  95. return 0;
  96. }
  97. /// <summary>
  98. /// Returns a string that represents the value of this <see cref="ValueTuple"/> instance.
  99. /// </summary>
  100. /// <returns>The string representation of this <see cref="ValueTuple"/> instance.</returns>
  101. /// <remarks>
  102. /// The string returned by this method takes the form <c>()</c>.
  103. /// </remarks>
  104. public override string ToString()
  105. {
  106. return "()";
  107. }
  108. string IValueTupleInternal.ToStringEnd()
  109. {
  110. return ")";
  111. }
  112. /// <summary>
  113. /// The number of positions in this data structure.
  114. /// </summary>
  115. int ITuple.Length => 0;
  116. /// <summary>
  117. /// Get the element at position <param name="index"/>.
  118. /// </summary>
  119. object? ITuple.this[int index] => throw new IndexOutOfRangeException();
  120. /// <summary>Creates a new struct 0-tuple.</summary>
  121. /// <returns>A 0-tuple.</returns>
  122. public static ValueTuple Create() =>
  123. default;
  124. /// <summary>Creates a new struct 1-tuple, or singleton.</summary>
  125. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  126. /// <param name="item1">The value of the first component of the tuple.</param>
  127. /// <returns>A 1-tuple (singleton) whose value is (item1).</returns>
  128. public static ValueTuple<T1> Create<T1>(T1 item1) =>
  129. new ValueTuple<T1>(item1);
  130. /// <summary>Creates a new struct 2-tuple, or pair.</summary>
  131. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  132. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  133. /// <param name="item1">The value of the first component of the tuple.</param>
  134. /// <param name="item2">The value of the second component of the tuple.</param>
  135. /// <returns>A 2-tuple (pair) whose value is (item1, item2).</returns>
  136. public static ValueTuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2) =>
  137. new ValueTuple<T1, T2>(item1, item2);
  138. /// <summary>Creates a new struct 3-tuple, or triple.</summary>
  139. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  140. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  141. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  142. /// <param name="item1">The value of the first component of the tuple.</param>
  143. /// <param name="item2">The value of the second component of the tuple.</param>
  144. /// <param name="item3">The value of the third component of the tuple.</param>
  145. /// <returns>A 3-tuple (triple) whose value is (item1, item2, item3).</returns>
  146. public static ValueTuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3) =>
  147. new ValueTuple<T1, T2, T3>(item1, item2, item3);
  148. /// <summary>Creates a new struct 4-tuple, or quadruple.</summary>
  149. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  150. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  151. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  152. /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam>
  153. /// <param name="item1">The value of the first component of the tuple.</param>
  154. /// <param name="item2">The value of the second component of the tuple.</param>
  155. /// <param name="item3">The value of the third component of the tuple.</param>
  156. /// <param name="item4">The value of the fourth component of the tuple.</param>
  157. /// <returns>A 4-tuple (quadruple) whose value is (item1, item2, item3, item4).</returns>
  158. public static ValueTuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 item1, T2 item2, T3 item3, T4 item4) =>
  159. new ValueTuple<T1, T2, T3, T4>(item1, item2, item3, item4);
  160. /// <summary>Creates a new struct 5-tuple, or quintuple.</summary>
  161. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  162. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  163. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  164. /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam>
  165. /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam>
  166. /// <param name="item1">The value of the first component of the tuple.</param>
  167. /// <param name="item2">The value of the second component of the tuple.</param>
  168. /// <param name="item3">The value of the third component of the tuple.</param>
  169. /// <param name="item4">The value of the fourth component of the tuple.</param>
  170. /// <param name="item5">The value of the fifth component of the tuple.</param>
  171. /// <returns>A 5-tuple (quintuple) whose value is (item1, item2, item3, item4, item5).</returns>
  172. public static ValueTuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5) =>
  173. new ValueTuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
  174. /// <summary>Creates a new struct 6-tuple, or sextuple.</summary>
  175. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  176. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  177. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  178. /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam>
  179. /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam>
  180. /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam>
  181. /// <param name="item1">The value of the first component of the tuple.</param>
  182. /// <param name="item2">The value of the second component of the tuple.</param>
  183. /// <param name="item3">The value of the third component of the tuple.</param>
  184. /// <param name="item4">The value of the fourth component of the tuple.</param>
  185. /// <param name="item5">The value of the fifth component of the tuple.</param>
  186. /// <param name="item6">The value of the sixth component of the tuple.</param>
  187. /// <returns>A 6-tuple (sextuple) whose value is (item1, item2, item3, item4, item5, item6).</returns>
  188. public static ValueTuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6) =>
  189. new ValueTuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
  190. /// <summary>Creates a new struct 7-tuple, or septuple.</summary>
  191. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  192. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  193. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  194. /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam>
  195. /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam>
  196. /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam>
  197. /// <typeparam name="T7">The type of the seventh component of the tuple.</typeparam>
  198. /// <param name="item1">The value of the first component of the tuple.</param>
  199. /// <param name="item2">The value of the second component of the tuple.</param>
  200. /// <param name="item3">The value of the third component of the tuple.</param>
  201. /// <param name="item4">The value of the fourth component of the tuple.</param>
  202. /// <param name="item5">The value of the fifth component of the tuple.</param>
  203. /// <param name="item6">The value of the sixth component of the tuple.</param>
  204. /// <param name="item7">The value of the seventh component of the tuple.</param>
  205. /// <returns>A 7-tuple (septuple) whose value is (item1, item2, item3, item4, item5, item6, item7).</returns>
  206. public static ValueTuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7) =>
  207. new ValueTuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
  208. /// <summary>Creates a new struct 8-tuple, or octuple.</summary>
  209. /// <typeparam name="T1">The type of the first component of the tuple.</typeparam>
  210. /// <typeparam name="T2">The type of the second component of the tuple.</typeparam>
  211. /// <typeparam name="T3">The type of the third component of the tuple.</typeparam>
  212. /// <typeparam name="T4">The type of the fourth component of the tuple.</typeparam>
  213. /// <typeparam name="T5">The type of the fifth component of the tuple.</typeparam>
  214. /// <typeparam name="T6">The type of the sixth component of the tuple.</typeparam>
  215. /// <typeparam name="T7">The type of the seventh component of the tuple.</typeparam>
  216. /// <typeparam name="T8">The type of the eighth component of the tuple.</typeparam>
  217. /// <param name="item1">The value of the first component of the tuple.</param>
  218. /// <param name="item2">The value of the second component of the tuple.</param>
  219. /// <param name="item3">The value of the third component of the tuple.</param>
  220. /// <param name="item4">The value of the fourth component of the tuple.</param>
  221. /// <param name="item5">The value of the fifth component of the tuple.</param>
  222. /// <param name="item6">The value of the sixth component of the tuple.</param>
  223. /// <param name="item7">The value of the seventh component of the tuple.</param>
  224. /// <param name="item8">The value of the eighth component of the tuple.</param>
  225. /// <returns>An 8-tuple (octuple) whose value is (item1, item2, item3, item4, item5, item6, item7, item8).</returns>
  226. public static ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>> Create<T1, T2, T3, T4, T5, T6, T7, T8>(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, T8 item8) =>
  227. new ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>(item1, item2, item3, item4, item5, item6, item7, ValueTuple.Create(item8));
  228. }
  229. /// <summary>Represents a 1-tuple, or singleton, as a value type.</summary>
  230. /// <typeparam name="T1">The type of the tuple's only component.</typeparam>
  231. [Serializable]
  232. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  233. public struct ValueTuple<T1>
  234. : IEquatable<ValueTuple<T1>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1>>, IValueTupleInternal, ITuple
  235. {
  236. /// <summary>
  237. /// The current <see cref="ValueTuple{T1}"/> instance's first component.
  238. /// </summary>
  239. public T1 Item1;
  240. /// <summary>
  241. /// Initializes a new instance of the <see cref="ValueTuple{T1}"/> value type.
  242. /// </summary>
  243. /// <param name="item1">The value of the tuple's first component.</param>
  244. public ValueTuple(T1 item1)
  245. {
  246. Item1 = item1;
  247. }
  248. /// <summary>
  249. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1}"/> instance is equal to a specified object.
  250. /// </summary>
  251. /// <param name="obj">The object to compare with this instance.</param>
  252. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  253. /// <remarks>
  254. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  255. /// <list type="bullet">
  256. /// <item><description>It is a <see cref="ValueTuple{T1}"/> value type.</description></item>
  257. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  258. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  259. /// </list>
  260. /// </remarks>
  261. public override bool Equals(object? obj)
  262. {
  263. return obj is ValueTuple<T1> && Equals((ValueTuple<T1>)obj);
  264. }
  265. /// <summary>
  266. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1}"/>
  267. /// instance is equal to a specified <see cref="ValueTuple{T1}"/>.
  268. /// </summary>
  269. /// <param name="other">The tuple to compare with this instance.</param>
  270. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  271. /// <remarks>
  272. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its field
  273. /// is equal to that of the current instance, using the default comparer for that field's type.
  274. /// </remarks>
  275. public bool Equals(ValueTuple<T1> other)
  276. {
  277. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1);
  278. }
  279. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  280. {
  281. if (other == null || !(other is ValueTuple<T1>)) return false;
  282. var objTuple = (ValueTuple<T1>)other;
  283. return comparer.Equals(Item1, objTuple.Item1);
  284. }
  285. int IComparable.CompareTo(object? other)
  286. {
  287. if (other == null) return 1;
  288. if (!(other is ValueTuple<T1>))
  289. {
  290. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  291. }
  292. var objTuple = (ValueTuple<T1>)other;
  293. return Comparer<T1>.Default.Compare(Item1, objTuple.Item1);
  294. }
  295. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  296. /// <param name="other">An instance to compare.</param>
  297. /// <returns>
  298. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  299. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  300. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  301. /// than <paramref name="other"/>.
  302. /// </returns>
  303. public int CompareTo(ValueTuple<T1> other)
  304. {
  305. return Comparer<T1>.Default.Compare(Item1, other.Item1);
  306. }
  307. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  308. {
  309. if (other == null) return 1;
  310. if (!(other is ValueTuple<T1>))
  311. {
  312. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  313. }
  314. var objTuple = (ValueTuple<T1>)other;
  315. return comparer.Compare(Item1, objTuple.Item1);
  316. }
  317. /// <summary>
  318. /// Returns the hash code for the current <see cref="ValueTuple{T1}"/> instance.
  319. /// </summary>
  320. /// <returns>A 32-bit signed integer hash code.</returns>
  321. public override int GetHashCode()
  322. {
  323. return Item1?.GetHashCode() ?? 0;
  324. }
  325. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  326. {
  327. return comparer.GetHashCode(Item1!);
  328. }
  329. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  330. {
  331. return comparer.GetHashCode(Item1!);
  332. }
  333. /// <summary>
  334. /// Returns a string that represents the value of this <see cref="ValueTuple{T1}"/> instance.
  335. /// </summary>
  336. /// <returns>The string representation of this <see cref="ValueTuple{T1}"/> instance.</returns>
  337. /// <remarks>
  338. /// The string returned by this method takes the form <c>(Item1)</c>,
  339. /// where <c>Item1</c> represents the value of <see cref="Item1"/>. If the field is <see langword="null"/>,
  340. /// it is represented as <see cref="string.Empty"/>.
  341. /// </remarks>
  342. public override string ToString()
  343. {
  344. return "(" + Item1?.ToString() + ")";
  345. }
  346. string IValueTupleInternal.ToStringEnd()
  347. {
  348. return Item1?.ToString() + ")";
  349. }
  350. /// <summary>
  351. /// The number of positions in this data structure.
  352. /// </summary>
  353. int ITuple.Length => 1;
  354. /// <summary>
  355. /// Get the element at position <param name="index"/>.
  356. /// </summary>
  357. object? ITuple.this[int index]
  358. {
  359. get
  360. {
  361. if (index != 0)
  362. {
  363. throw new IndexOutOfRangeException();
  364. }
  365. return Item1;
  366. }
  367. }
  368. }
  369. /// <summary>
  370. /// Represents a 2-tuple, or pair, as a value type.
  371. /// </summary>
  372. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  373. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  374. [Serializable]
  375. [StructLayout(LayoutKind.Auto)]
  376. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  377. public struct ValueTuple<T1, T2>
  378. : IEquatable<ValueTuple<T1, T2>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2>>, IValueTupleInternal, ITuple
  379. {
  380. /// <summary>
  381. /// The current <see cref="ValueTuple{T1, T2}"/> instance's first component.
  382. /// </summary>
  383. public T1 Item1;
  384. /// <summary>
  385. /// The current <see cref="ValueTuple{T1, T2}"/> instance's second component.
  386. /// </summary>
  387. public T2 Item2;
  388. /// <summary>
  389. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2}"/> value type.
  390. /// </summary>
  391. /// <param name="item1">The value of the tuple's first component.</param>
  392. /// <param name="item2">The value of the tuple's second component.</param>
  393. public ValueTuple(T1 item1, T2 item2)
  394. {
  395. Item1 = item1;
  396. Item2 = item2;
  397. }
  398. /// <summary>
  399. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2}"/> instance is equal to a specified object.
  400. /// </summary>
  401. /// <param name="obj">The object to compare with this instance.</param>
  402. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  403. ///
  404. /// <remarks>
  405. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  406. /// <list type="bullet">
  407. /// <item><description>It is a <see cref="ValueTuple{T1, T2}"/> value type.</description></item>
  408. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  409. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  410. /// </list>
  411. /// </remarks>
  412. public override bool Equals(object? obj)
  413. {
  414. return obj is ValueTuple<T1, T2> && Equals((ValueTuple<T1, T2>)obj);
  415. }
  416. /// <summary>
  417. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2}"/> instance is equal to a specified <see cref="ValueTuple{T1, T2}"/>.
  418. /// </summary>
  419. /// <param name="other">The tuple to compare with this instance.</param>
  420. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  421. /// <remarks>
  422. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  423. /// are equal to that of the current instance, using the default comparer for that field's type.
  424. /// </remarks>
  425. public bool Equals(ValueTuple<T1, T2> other)
  426. {
  427. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  428. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2);
  429. }
  430. /// <summary>
  431. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2}"/> instance is equal to a specified object based on a specified comparison method.
  432. /// </summary>
  433. /// <param name="other">The object to compare with this instance.</param>
  434. /// <param name="comparer">An object that defines the method to use to evaluate whether the two objects are equal.</param>
  435. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  436. ///
  437. /// <remarks>
  438. /// This member is an explicit interface member implementation. It can be used only when the
  439. /// <see cref="ValueTuple{T1, T2}"/> instance is cast to an <see cref="IStructuralEquatable"/> interface.
  440. ///
  441. /// The <see cref="IEqualityComparer.Equals"/> implementation is called only if <c>other</c> is not <see langword="null"/>,
  442. /// and if it can be successfully cast (in C#) or converted (in Visual Basic) to a <see cref="ValueTuple{T1, T2}"/>
  443. /// whose components are of the same types as those of the current instance. The IStructuralEquatable.Equals(Object, IEqualityComparer) method
  444. /// first passes the <see cref="Item1"/> values of the <see cref="ValueTuple{T1, T2}"/> objects to be compared to the
  445. /// <see cref="IEqualityComparer.Equals"/> implementation. If this method call returns <see langword="true"/>, the method is
  446. /// called again and passed the <see cref="Item2"/> values of the two <see cref="ValueTuple{T1, T2}"/> instances.
  447. /// </remarks>
  448. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  449. {
  450. if (other == null || !(other is ValueTuple<T1, T2>)) return false;
  451. var objTuple = (ValueTuple<T1, T2>)other;
  452. return comparer.Equals(Item1, objTuple.Item1)
  453. && comparer.Equals(Item2, objTuple.Item2);
  454. }
  455. int IComparable.CompareTo(object? other)
  456. {
  457. if (other == null) return 1;
  458. if (!(other is ValueTuple<T1, T2>))
  459. {
  460. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  461. }
  462. return CompareTo((ValueTuple<T1, T2>)other);
  463. }
  464. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  465. /// <param name="other">An instance to compare.</param>
  466. /// <returns>
  467. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  468. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  469. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  470. /// than <paramref name="other"/>.
  471. /// </returns>
  472. public int CompareTo(ValueTuple<T1, T2> other)
  473. {
  474. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  475. if (c != 0) return c;
  476. return Comparer<T2>.Default.Compare(Item2, other.Item2);
  477. }
  478. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  479. {
  480. if (other == null) return 1;
  481. if (!(other is ValueTuple<T1, T2>))
  482. {
  483. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  484. }
  485. var objTuple = (ValueTuple<T1, T2>)other;
  486. int c = comparer.Compare(Item1, objTuple.Item1);
  487. if (c != 0) return c;
  488. return comparer.Compare(Item2, objTuple.Item2);
  489. }
  490. /// <summary>
  491. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2}"/> instance.
  492. /// </summary>
  493. /// <returns>A 32-bit signed integer hash code.</returns>
  494. public override int GetHashCode()
  495. {
  496. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  497. Item2?.GetHashCode() ?? 0);
  498. }
  499. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  500. {
  501. return GetHashCodeCore(comparer);
  502. }
  503. private int GetHashCodeCore(IEqualityComparer comparer)
  504. {
  505. return HashCode.Combine(comparer.GetHashCode(Item1!),
  506. comparer.GetHashCode(Item2!));
  507. }
  508. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  509. {
  510. return GetHashCodeCore(comparer);
  511. }
  512. /// <summary>
  513. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2}"/> instance.
  514. /// </summary>
  515. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2}"/> instance.</returns>
  516. /// <remarks>
  517. /// The string returned by this method takes the form <c>(Item1, Item2)</c>,
  518. /// where <c>Item1</c> and <c>Item2</c> represent the values of the <see cref="Item1"/>
  519. /// and <see cref="Item2"/> fields. If either field value is <see langword="null"/>,
  520. /// it is represented as <see cref="string.Empty"/>.
  521. /// </remarks>
  522. public override string ToString()
  523. {
  524. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ")";
  525. }
  526. string IValueTupleInternal.ToStringEnd()
  527. {
  528. return Item1?.ToString() + ", " + Item2?.ToString() + ")";
  529. }
  530. /// <summary>
  531. /// The number of positions in this data structure.
  532. /// </summary>
  533. int ITuple.Length => 2;
  534. /// <summary>
  535. /// Get the element at position <param name="index"/>.
  536. /// </summary>
  537. object? ITuple.this[int index] =>
  538. index switch
  539. {
  540. 0 => Item1,
  541. 1 => Item2,
  542. _ => throw new IndexOutOfRangeException(),
  543. };
  544. }
  545. /// <summary>
  546. /// Represents a 3-tuple, or triple, as a value type.
  547. /// </summary>
  548. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  549. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  550. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  551. [Serializable]
  552. [StructLayout(LayoutKind.Auto)]
  553. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  554. public struct ValueTuple<T1, T2, T3>
  555. : IEquatable<ValueTuple<T1, T2, T3>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3>>, IValueTupleInternal, ITuple
  556. {
  557. /// <summary>
  558. /// The current <see cref="ValueTuple{T1, T2, T3}"/> instance's first component.
  559. /// </summary>
  560. public T1 Item1;
  561. /// <summary>
  562. /// The current <see cref="ValueTuple{T1, T2, T3}"/> instance's second component.
  563. /// </summary>
  564. public T2 Item2;
  565. /// <summary>
  566. /// The current <see cref="ValueTuple{T1, T2, T3}"/> instance's third component.
  567. /// </summary>
  568. public T3 Item3;
  569. /// <summary>
  570. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3}"/> value type.
  571. /// </summary>
  572. /// <param name="item1">The value of the tuple's first component.</param>
  573. /// <param name="item2">The value of the tuple's second component.</param>
  574. /// <param name="item3">The value of the tuple's third component.</param>
  575. public ValueTuple(T1 item1, T2 item2, T3 item3)
  576. {
  577. Item1 = item1;
  578. Item2 = item2;
  579. Item3 = item3;
  580. }
  581. /// <summary>
  582. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3}"/> instance is equal to a specified object.
  583. /// </summary>
  584. /// <param name="obj">The object to compare with this instance.</param>
  585. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  586. /// <remarks>
  587. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  588. /// <list type="bullet">
  589. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3}"/> value type.</description></item>
  590. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  591. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  592. /// </list>
  593. /// </remarks>
  594. public override bool Equals(object? obj)
  595. {
  596. return obj is ValueTuple<T1, T2, T3> && Equals((ValueTuple<T1, T2, T3>)obj);
  597. }
  598. /// <summary>
  599. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3}"/>
  600. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3}"/>.
  601. /// </summary>
  602. /// <param name="other">The tuple to compare with this instance.</param>
  603. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  604. /// <remarks>
  605. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  606. /// are equal to that of the current instance, using the default comparer for that field's type.
  607. /// </remarks>
  608. public bool Equals(ValueTuple<T1, T2, T3> other)
  609. {
  610. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  611. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  612. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3);
  613. }
  614. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  615. {
  616. if (other == null || !(other is ValueTuple<T1, T2, T3>)) return false;
  617. var objTuple = (ValueTuple<T1, T2, T3>)other;
  618. return comparer.Equals(Item1, objTuple.Item1)
  619. && comparer.Equals(Item2, objTuple.Item2)
  620. && comparer.Equals(Item3, objTuple.Item3);
  621. }
  622. int IComparable.CompareTo(object? other)
  623. {
  624. if (other == null) return 1;
  625. if (!(other is ValueTuple<T1, T2, T3>))
  626. {
  627. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  628. }
  629. return CompareTo((ValueTuple<T1, T2, T3>)other);
  630. }
  631. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  632. /// <param name="other">An instance to compare.</param>
  633. /// <returns>
  634. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  635. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  636. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  637. /// than <paramref name="other"/>.
  638. /// </returns>
  639. public int CompareTo(ValueTuple<T1, T2, T3> other)
  640. {
  641. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  642. if (c != 0) return c;
  643. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  644. if (c != 0) return c;
  645. return Comparer<T3>.Default.Compare(Item3, other.Item3);
  646. }
  647. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  648. {
  649. if (other == null) return 1;
  650. if (!(other is ValueTuple<T1, T2, T3>))
  651. {
  652. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  653. }
  654. var objTuple = (ValueTuple<T1, T2, T3>)other;
  655. int c = comparer.Compare(Item1, objTuple.Item1);
  656. if (c != 0) return c;
  657. c = comparer.Compare(Item2, objTuple.Item2);
  658. if (c != 0) return c;
  659. return comparer.Compare(Item3, objTuple.Item3);
  660. }
  661. /// <summary>
  662. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3}"/> instance.
  663. /// </summary>
  664. /// <returns>A 32-bit signed integer hash code.</returns>
  665. public override int GetHashCode()
  666. {
  667. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  668. Item2?.GetHashCode() ?? 0,
  669. Item3?.GetHashCode() ?? 0);
  670. }
  671. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  672. {
  673. return GetHashCodeCore(comparer);
  674. }
  675. private int GetHashCodeCore(IEqualityComparer comparer)
  676. {
  677. return HashCode.Combine(comparer.GetHashCode(Item1!),
  678. comparer.GetHashCode(Item2!),
  679. comparer.GetHashCode(Item3!));
  680. }
  681. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  682. {
  683. return GetHashCodeCore(comparer);
  684. }
  685. /// <summary>
  686. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3}"/> instance.
  687. /// </summary>
  688. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3}"/> instance.</returns>
  689. /// <remarks>
  690. /// The string returned by this method takes the form <c>(Item1, Item2, Item3)</c>.
  691. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  692. /// </remarks>
  693. public override string ToString()
  694. {
  695. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ")";
  696. }
  697. string IValueTupleInternal.ToStringEnd()
  698. {
  699. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ")";
  700. }
  701. /// <summary>
  702. /// The number of positions in this data structure.
  703. /// </summary>
  704. int ITuple.Length => 3;
  705. /// <summary>
  706. /// Get the element at position <param name="index"/>.
  707. /// </summary>
  708. object? ITuple.this[int index] =>
  709. index switch
  710. {
  711. 0 => Item1,
  712. 1 => Item2,
  713. 2 => Item3,
  714. _ => throw new IndexOutOfRangeException(),
  715. };
  716. }
  717. /// <summary>
  718. /// Represents a 4-tuple, or quadruple, as a value type.
  719. /// </summary>
  720. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  721. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  722. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  723. /// <typeparam name="T4">The type of the tuple's fourth component.</typeparam>
  724. [Serializable]
  725. [StructLayout(LayoutKind.Auto)]
  726. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  727. public struct ValueTuple<T1, T2, T3, T4>
  728. : IEquatable<ValueTuple<T1, T2, T3, T4>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4>>, IValueTupleInternal, ITuple
  729. {
  730. /// <summary>
  731. /// The current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance's first component.
  732. /// </summary>
  733. public T1 Item1;
  734. /// <summary>
  735. /// The current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance's second component.
  736. /// </summary>
  737. public T2 Item2;
  738. /// <summary>
  739. /// The current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance's third component.
  740. /// </summary>
  741. public T3 Item3;
  742. /// <summary>
  743. /// The current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance's fourth component.
  744. /// </summary>
  745. public T4 Item4;
  746. /// <summary>
  747. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3, T4}"/> value type.
  748. /// </summary>
  749. /// <param name="item1">The value of the tuple's first component.</param>
  750. /// <param name="item2">The value of the tuple's second component.</param>
  751. /// <param name="item3">The value of the tuple's third component.</param>
  752. /// <param name="item4">The value of the tuple's fourth component.</param>
  753. public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4)
  754. {
  755. Item1 = item1;
  756. Item2 = item2;
  757. Item3 = item3;
  758. Item4 = item4;
  759. }
  760. /// <summary>
  761. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance is equal to a specified object.
  762. /// </summary>
  763. /// <param name="obj">The object to compare with this instance.</param>
  764. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  765. /// <remarks>
  766. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  767. /// <list type="bullet">
  768. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3, T4}"/> value type.</description></item>
  769. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  770. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  771. /// </list>
  772. /// </remarks>
  773. public override bool Equals(object? obj)
  774. {
  775. return obj is ValueTuple<T1, T2, T3, T4> && Equals((ValueTuple<T1, T2, T3, T4>)obj);
  776. }
  777. /// <summary>
  778. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4}"/>
  779. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3, T4}"/>.
  780. /// </summary>
  781. /// <param name="other">The tuple to compare with this instance.</param>
  782. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  783. /// <remarks>
  784. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  785. /// are equal to that of the current instance, using the default comparer for that field's type.
  786. /// </remarks>
  787. public bool Equals(ValueTuple<T1, T2, T3, T4> other)
  788. {
  789. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  790. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  791. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3)
  792. && EqualityComparer<T4>.Default.Equals(Item4, other.Item4);
  793. }
  794. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  795. {
  796. if (other == null || !(other is ValueTuple<T1, T2, T3, T4>)) return false;
  797. var objTuple = (ValueTuple<T1, T2, T3, T4>)other;
  798. return comparer.Equals(Item1, objTuple.Item1)
  799. && comparer.Equals(Item2, objTuple.Item2)
  800. && comparer.Equals(Item3, objTuple.Item3)
  801. && comparer.Equals(Item4, objTuple.Item4);
  802. }
  803. int IComparable.CompareTo(object? other)
  804. {
  805. if (other == null) return 1;
  806. if (!(other is ValueTuple<T1, T2, T3, T4>))
  807. {
  808. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  809. }
  810. return CompareTo((ValueTuple<T1, T2, T3, T4>)other);
  811. }
  812. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  813. /// <param name="other">An instance to compare.</param>
  814. /// <returns>
  815. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  816. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  817. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  818. /// than <paramref name="other"/>.
  819. /// </returns>
  820. public int CompareTo(ValueTuple<T1, T2, T3, T4> other)
  821. {
  822. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  823. if (c != 0) return c;
  824. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  825. if (c != 0) return c;
  826. c = Comparer<T3>.Default.Compare(Item3, other.Item3);
  827. if (c != 0) return c;
  828. return Comparer<T4>.Default.Compare(Item4, other.Item4);
  829. }
  830. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  831. {
  832. if (other == null) return 1;
  833. if (!(other is ValueTuple<T1, T2, T3, T4>))
  834. {
  835. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  836. }
  837. var objTuple = (ValueTuple<T1, T2, T3, T4>)other;
  838. int c = comparer.Compare(Item1, objTuple.Item1);
  839. if (c != 0) return c;
  840. c = comparer.Compare(Item2, objTuple.Item2);
  841. if (c != 0) return c;
  842. c = comparer.Compare(Item3, objTuple.Item3);
  843. if (c != 0) return c;
  844. return comparer.Compare(Item4, objTuple.Item4);
  845. }
  846. /// <summary>
  847. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3, T4}"/> instance.
  848. /// </summary>
  849. /// <returns>A 32-bit signed integer hash code.</returns>
  850. public override int GetHashCode()
  851. {
  852. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  853. Item2?.GetHashCode() ?? 0,
  854. Item3?.GetHashCode() ?? 0,
  855. Item4?.GetHashCode() ?? 0);
  856. }
  857. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  858. {
  859. return GetHashCodeCore(comparer);
  860. }
  861. private int GetHashCodeCore(IEqualityComparer comparer)
  862. {
  863. return HashCode.Combine(comparer.GetHashCode(Item1!),
  864. comparer.GetHashCode(Item2!),
  865. comparer.GetHashCode(Item3!),
  866. comparer.GetHashCode(Item4!));
  867. }
  868. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  869. {
  870. return GetHashCodeCore(comparer);
  871. }
  872. /// <summary>
  873. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3, T4}"/> instance.
  874. /// </summary>
  875. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3, T4}"/> instance.</returns>
  876. /// <remarks>
  877. /// The string returned by this method takes the form <c>(Item1, Item2, Item3, Item4)</c>.
  878. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  879. /// </remarks>
  880. public override string ToString()
  881. {
  882. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ")";
  883. }
  884. string IValueTupleInternal.ToStringEnd()
  885. {
  886. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ")";
  887. }
  888. /// <summary>
  889. /// The number of positions in this data structure.
  890. /// </summary>
  891. int ITuple.Length => 4;
  892. /// <summary>
  893. /// Get the element at position <param name="index"/>.
  894. /// </summary>
  895. object? ITuple.this[int index] =>
  896. index switch
  897. {
  898. 0 => Item1,
  899. 1 => Item2,
  900. 2 => Item3,
  901. 3 => Item4,
  902. _ => throw new IndexOutOfRangeException(),
  903. };
  904. }
  905. /// <summary>
  906. /// Represents a 5-tuple, or quintuple, as a value type.
  907. /// </summary>
  908. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  909. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  910. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  911. /// <typeparam name="T4">The type of the tuple's fourth component.</typeparam>
  912. /// <typeparam name="T5">The type of the tuple's fifth component.</typeparam>
  913. [Serializable]
  914. [StructLayout(LayoutKind.Auto)]
  915. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  916. public struct ValueTuple<T1, T2, T3, T4, T5>
  917. : IEquatable<ValueTuple<T1, T2, T3, T4, T5>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4, T5>>, IValueTupleInternal, ITuple
  918. {
  919. /// <summary>
  920. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance's first component.
  921. /// </summary>
  922. public T1 Item1;
  923. /// <summary>
  924. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance's second component.
  925. /// </summary>
  926. public T2 Item2;
  927. /// <summary>
  928. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance's third component.
  929. /// </summary>
  930. public T3 Item3;
  931. /// <summary>
  932. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance's fourth component.
  933. /// </summary>
  934. public T4 Item4;
  935. /// <summary>
  936. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance's fifth component.
  937. /// </summary>
  938. public T5 Item5;
  939. /// <summary>
  940. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> value type.
  941. /// </summary>
  942. /// <param name="item1">The value of the tuple's first component.</param>
  943. /// <param name="item2">The value of the tuple's second component.</param>
  944. /// <param name="item3">The value of the tuple's third component.</param>
  945. /// <param name="item4">The value of the tuple's fourth component.</param>
  946. /// <param name="item5">The value of the tuple's fifth component.</param>
  947. public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5)
  948. {
  949. Item1 = item1;
  950. Item2 = item2;
  951. Item3 = item3;
  952. Item4 = item4;
  953. Item5 = item5;
  954. }
  955. /// <summary>
  956. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance is equal to a specified object.
  957. /// </summary>
  958. /// <param name="obj">The object to compare with this instance.</param>
  959. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  960. /// <remarks>
  961. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  962. /// <list type="bullet">
  963. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> value type.</description></item>
  964. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  965. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  966. /// </list>
  967. /// </remarks>
  968. public override bool Equals(object? obj)
  969. {
  970. return obj is ValueTuple<T1, T2, T3, T4, T5> && Equals((ValueTuple<T1, T2, T3, T4, T5>)obj);
  971. }
  972. /// <summary>
  973. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/>
  974. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3, T4, T5}"/>.
  975. /// </summary>
  976. /// <param name="other">The tuple to compare with this instance.</param>
  977. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  978. /// <remarks>
  979. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  980. /// are equal to that of the current instance, using the default comparer for that field's type.
  981. /// </remarks>
  982. public bool Equals(ValueTuple<T1, T2, T3, T4, T5> other)
  983. {
  984. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  985. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  986. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3)
  987. && EqualityComparer<T4>.Default.Equals(Item4, other.Item4)
  988. && EqualityComparer<T5>.Default.Equals(Item5, other.Item5);
  989. }
  990. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  991. {
  992. if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5>)) return false;
  993. var objTuple = (ValueTuple<T1, T2, T3, T4, T5>)other;
  994. return comparer.Equals(Item1, objTuple.Item1)
  995. && comparer.Equals(Item2, objTuple.Item2)
  996. && comparer.Equals(Item3, objTuple.Item3)
  997. && comparer.Equals(Item4, objTuple.Item4)
  998. && comparer.Equals(Item5, objTuple.Item5);
  999. }
  1000. int IComparable.CompareTo(object? other)
  1001. {
  1002. if (other == null) return 1;
  1003. if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
  1004. {
  1005. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1006. }
  1007. return CompareTo((ValueTuple<T1, T2, T3, T4, T5>)other);
  1008. }
  1009. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  1010. /// <param name="other">An instance to compare.</param>
  1011. /// <returns>
  1012. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  1013. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  1014. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  1015. /// than <paramref name="other"/>.
  1016. /// </returns>
  1017. public int CompareTo(ValueTuple<T1, T2, T3, T4, T5> other)
  1018. {
  1019. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  1020. if (c != 0) return c;
  1021. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  1022. if (c != 0) return c;
  1023. c = Comparer<T3>.Default.Compare(Item3, other.Item3);
  1024. if (c != 0) return c;
  1025. c = Comparer<T4>.Default.Compare(Item4, other.Item4);
  1026. if (c != 0) return c;
  1027. return Comparer<T5>.Default.Compare(Item5, other.Item5);
  1028. }
  1029. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  1030. {
  1031. if (other == null) return 1;
  1032. if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
  1033. {
  1034. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1035. }
  1036. var objTuple = (ValueTuple<T1, T2, T3, T4, T5>)other;
  1037. int c = comparer.Compare(Item1, objTuple.Item1);
  1038. if (c != 0) return c;
  1039. c = comparer.Compare(Item2, objTuple.Item2);
  1040. if (c != 0) return c;
  1041. c = comparer.Compare(Item3, objTuple.Item3);
  1042. if (c != 0) return c;
  1043. c = comparer.Compare(Item4, objTuple.Item4);
  1044. if (c != 0) return c;
  1045. return comparer.Compare(Item5, objTuple.Item5);
  1046. }
  1047. /// <summary>
  1048. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance.
  1049. /// </summary>
  1050. /// <returns>A 32-bit signed integer hash code.</returns>
  1051. public override int GetHashCode()
  1052. {
  1053. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  1054. Item2?.GetHashCode() ?? 0,
  1055. Item3?.GetHashCode() ?? 0,
  1056. Item4?.GetHashCode() ?? 0,
  1057. Item5?.GetHashCode() ?? 0);
  1058. }
  1059. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  1060. {
  1061. return GetHashCodeCore(comparer);
  1062. }
  1063. private int GetHashCodeCore(IEqualityComparer comparer)
  1064. {
  1065. return HashCode.Combine(comparer.GetHashCode(Item1!),
  1066. comparer.GetHashCode(Item2!),
  1067. comparer.GetHashCode(Item3!),
  1068. comparer.GetHashCode(Item4!),
  1069. comparer.GetHashCode(Item5!));
  1070. }
  1071. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  1072. {
  1073. return GetHashCodeCore(comparer);
  1074. }
  1075. /// <summary>
  1076. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance.
  1077. /// </summary>
  1078. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3, T4, T5}"/> instance.</returns>
  1079. /// <remarks>
  1080. /// The string returned by this method takes the form <c>(Item1, Item2, Item3, Item4, Item5)</c>.
  1081. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  1082. /// </remarks>
  1083. public override string ToString()
  1084. {
  1085. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ")";
  1086. }
  1087. string IValueTupleInternal.ToStringEnd()
  1088. {
  1089. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ")";
  1090. }
  1091. /// <summary>
  1092. /// The number of positions in this data structure.
  1093. /// </summary>
  1094. int ITuple.Length => 5;
  1095. /// <summary>
  1096. /// Get the element at position <param name="index"/>.
  1097. /// </summary>
  1098. object? ITuple.this[int index] =>
  1099. index switch
  1100. {
  1101. 0 => Item1,
  1102. 1 => Item2,
  1103. 2 => Item3,
  1104. 3 => Item4,
  1105. 4 => Item5,
  1106. _ => throw new IndexOutOfRangeException(),
  1107. };
  1108. }
  1109. /// <summary>
  1110. /// Represents a 6-tuple, or sixtuple, as a value type.
  1111. /// </summary>
  1112. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  1113. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  1114. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  1115. /// <typeparam name="T4">The type of the tuple's fourth component.</typeparam>
  1116. /// <typeparam name="T5">The type of the tuple's fifth component.</typeparam>
  1117. /// <typeparam name="T6">The type of the tuple's sixth component.</typeparam>
  1118. [Serializable]
  1119. [StructLayout(LayoutKind.Auto)]
  1120. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  1121. public struct ValueTuple<T1, T2, T3, T4, T5, T6>
  1122. : IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4, T5, T6>>, IValueTupleInternal, ITuple
  1123. {
  1124. /// <summary>
  1125. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's first component.
  1126. /// </summary>
  1127. public T1 Item1;
  1128. /// <summary>
  1129. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's second component.
  1130. /// </summary>
  1131. public T2 Item2;
  1132. /// <summary>
  1133. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's third component.
  1134. /// </summary>
  1135. public T3 Item3;
  1136. /// <summary>
  1137. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's fourth component.
  1138. /// </summary>
  1139. public T4 Item4;
  1140. /// <summary>
  1141. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's fifth component.
  1142. /// </summary>
  1143. public T5 Item5;
  1144. /// <summary>
  1145. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance's sixth component.
  1146. /// </summary>
  1147. public T6 Item6;
  1148. /// <summary>
  1149. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> value type.
  1150. /// </summary>
  1151. /// <param name="item1">The value of the tuple's first component.</param>
  1152. /// <param name="item2">The value of the tuple's second component.</param>
  1153. /// <param name="item3">The value of the tuple's third component.</param>
  1154. /// <param name="item4">The value of the tuple's fourth component.</param>
  1155. /// <param name="item5">The value of the tuple's fifth component.</param>
  1156. /// <param name="item6">The value of the tuple's sixth component.</param>
  1157. public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6)
  1158. {
  1159. Item1 = item1;
  1160. Item2 = item2;
  1161. Item3 = item3;
  1162. Item4 = item4;
  1163. Item5 = item5;
  1164. Item6 = item6;
  1165. }
  1166. /// <summary>
  1167. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance is equal to a specified object.
  1168. /// </summary>
  1169. /// <param name="obj">The object to compare with this instance.</param>
  1170. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  1171. /// <remarks>
  1172. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  1173. /// <list type="bullet">
  1174. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> value type.</description></item>
  1175. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  1176. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  1177. /// </list>
  1178. /// </remarks>
  1179. public override bool Equals(object? obj)
  1180. {
  1181. return obj is ValueTuple<T1, T2, T3, T4, T5, T6> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6>)obj);
  1182. }
  1183. /// <summary>
  1184. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/>
  1185. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/>.
  1186. /// </summary>
  1187. /// <param name="other">The tuple to compare with this instance.</param>
  1188. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  1189. /// <remarks>
  1190. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  1191. /// are equal to that of the current instance, using the default comparer for that field's type.
  1192. /// </remarks>
  1193. public bool Equals(ValueTuple<T1, T2, T3, T4, T5, T6> other)
  1194. {
  1195. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  1196. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  1197. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3)
  1198. && EqualityComparer<T4>.Default.Equals(Item4, other.Item4)
  1199. && EqualityComparer<T5>.Default.Equals(Item5, other.Item5)
  1200. && EqualityComparer<T6>.Default.Equals(Item6, other.Item6);
  1201. }
  1202. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  1203. {
  1204. if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6>)) return false;
  1205. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6>)other;
  1206. return comparer.Equals(Item1, objTuple.Item1)
  1207. && comparer.Equals(Item2, objTuple.Item2)
  1208. && comparer.Equals(Item3, objTuple.Item3)
  1209. && comparer.Equals(Item4, objTuple.Item4)
  1210. && comparer.Equals(Item5, objTuple.Item5)
  1211. && comparer.Equals(Item6, objTuple.Item6);
  1212. }
  1213. int IComparable.CompareTo(object? other)
  1214. {
  1215. if (other == null) return 1;
  1216. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
  1217. {
  1218. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1219. }
  1220. return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6>)other);
  1221. }
  1222. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  1223. /// <param name="other">An instance to compare.</param>
  1224. /// <returns>
  1225. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  1226. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  1227. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  1228. /// than <paramref name="other"/>.
  1229. /// </returns>
  1230. public int CompareTo(ValueTuple<T1, T2, T3, T4, T5, T6> other)
  1231. {
  1232. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  1233. if (c != 0) return c;
  1234. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  1235. if (c != 0) return c;
  1236. c = Comparer<T3>.Default.Compare(Item3, other.Item3);
  1237. if (c != 0) return c;
  1238. c = Comparer<T4>.Default.Compare(Item4, other.Item4);
  1239. if (c != 0) return c;
  1240. c = Comparer<T5>.Default.Compare(Item5, other.Item5);
  1241. if (c != 0) return c;
  1242. return Comparer<T6>.Default.Compare(Item6, other.Item6);
  1243. }
  1244. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  1245. {
  1246. if (other == null) return 1;
  1247. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
  1248. {
  1249. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1250. }
  1251. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6>)other;
  1252. int c = comparer.Compare(Item1, objTuple.Item1);
  1253. if (c != 0) return c;
  1254. c = comparer.Compare(Item2, objTuple.Item2);
  1255. if (c != 0) return c;
  1256. c = comparer.Compare(Item3, objTuple.Item3);
  1257. if (c != 0) return c;
  1258. c = comparer.Compare(Item4, objTuple.Item4);
  1259. if (c != 0) return c;
  1260. c = comparer.Compare(Item5, objTuple.Item5);
  1261. if (c != 0) return c;
  1262. return comparer.Compare(Item6, objTuple.Item6);
  1263. }
  1264. /// <summary>
  1265. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance.
  1266. /// </summary>
  1267. /// <returns>A 32-bit signed integer hash code.</returns>
  1268. public override int GetHashCode()
  1269. {
  1270. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  1271. Item2?.GetHashCode() ?? 0,
  1272. Item3?.GetHashCode() ?? 0,
  1273. Item4?.GetHashCode() ?? 0,
  1274. Item5?.GetHashCode() ?? 0,
  1275. Item6?.GetHashCode() ?? 0);
  1276. }
  1277. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  1278. {
  1279. return GetHashCodeCore(comparer);
  1280. }
  1281. private int GetHashCodeCore(IEqualityComparer comparer)
  1282. {
  1283. return HashCode.Combine(comparer.GetHashCode(Item1!),
  1284. comparer.GetHashCode(Item2!),
  1285. comparer.GetHashCode(Item3!),
  1286. comparer.GetHashCode(Item4!),
  1287. comparer.GetHashCode(Item5!),
  1288. comparer.GetHashCode(Item6!));
  1289. }
  1290. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  1291. {
  1292. return GetHashCodeCore(comparer);
  1293. }
  1294. /// <summary>
  1295. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance.
  1296. /// </summary>
  1297. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6}"/> instance.</returns>
  1298. /// <remarks>
  1299. /// The string returned by this method takes the form <c>(Item1, Item2, Item3, Item4, Item5, Item6)</c>.
  1300. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  1301. /// </remarks>
  1302. public override string ToString()
  1303. {
  1304. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ")";
  1305. }
  1306. string IValueTupleInternal.ToStringEnd()
  1307. {
  1308. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ")";
  1309. }
  1310. /// <summary>
  1311. /// The number of positions in this data structure.
  1312. /// </summary>
  1313. int ITuple.Length => 6;
  1314. /// <summary>
  1315. /// Get the element at position <param name="index"/>.
  1316. /// </summary>
  1317. object? ITuple.this[int index] =>
  1318. index switch
  1319. {
  1320. 0 => Item1,
  1321. 1 => Item2,
  1322. 2 => Item3,
  1323. 3 => Item4,
  1324. 4 => Item5,
  1325. 5 => Item6,
  1326. _ => throw new IndexOutOfRangeException(),
  1327. };
  1328. }
  1329. /// <summary>
  1330. /// Represents a 7-tuple, or sentuple, as a value type.
  1331. /// </summary>
  1332. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  1333. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  1334. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  1335. /// <typeparam name="T4">The type of the tuple's fourth component.</typeparam>
  1336. /// <typeparam name="T5">The type of the tuple's fifth component.</typeparam>
  1337. /// <typeparam name="T6">The type of the tuple's sixth component.</typeparam>
  1338. /// <typeparam name="T7">The type of the tuple's seventh component.</typeparam>
  1339. [Serializable]
  1340. [StructLayout(LayoutKind.Auto)]
  1341. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  1342. public struct ValueTuple<T1, T2, T3, T4, T5, T6, T7>
  1343. : IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>, IValueTupleInternal, ITuple
  1344. {
  1345. /// <summary>
  1346. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's first component.
  1347. /// </summary>
  1348. public T1 Item1;
  1349. /// <summary>
  1350. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's second component.
  1351. /// </summary>
  1352. public T2 Item2;
  1353. /// <summary>
  1354. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's third component.
  1355. /// </summary>
  1356. public T3 Item3;
  1357. /// <summary>
  1358. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's fourth component.
  1359. /// </summary>
  1360. public T4 Item4;
  1361. /// <summary>
  1362. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's fifth component.
  1363. /// </summary>
  1364. public T5 Item5;
  1365. /// <summary>
  1366. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's sixth component.
  1367. /// </summary>
  1368. public T6 Item6;
  1369. /// <summary>
  1370. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance's seventh component.
  1371. /// </summary>
  1372. public T7 Item7;
  1373. /// <summary>
  1374. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> value type.
  1375. /// </summary>
  1376. /// <param name="item1">The value of the tuple's first component.</param>
  1377. /// <param name="item2">The value of the tuple's second component.</param>
  1378. /// <param name="item3">The value of the tuple's third component.</param>
  1379. /// <param name="item4">The value of the tuple's fourth component.</param>
  1380. /// <param name="item5">The value of the tuple's fifth component.</param>
  1381. /// <param name="item6">The value of the tuple's sixth component.</param>
  1382. /// <param name="item7">The value of the tuple's seventh component.</param>
  1383. public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7)
  1384. {
  1385. Item1 = item1;
  1386. Item2 = item2;
  1387. Item3 = item3;
  1388. Item4 = item4;
  1389. Item5 = item5;
  1390. Item6 = item6;
  1391. Item7 = item7;
  1392. }
  1393. /// <summary>
  1394. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance is equal to a specified object.
  1395. /// </summary>
  1396. /// <param name="obj">The object to compare with this instance.</param>
  1397. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  1398. /// <remarks>
  1399. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  1400. /// <list type="bullet">
  1401. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> value type.</description></item>
  1402. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  1403. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  1404. /// </list>
  1405. /// </remarks>
  1406. public override bool Equals(object? obj)
  1407. {
  1408. return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6, T7>)obj);
  1409. }
  1410. /// <summary>
  1411. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/>
  1412. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/>.
  1413. /// </summary>
  1414. /// <param name="other">The tuple to compare with this instance.</param>
  1415. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  1416. /// <remarks>
  1417. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  1418. /// are equal to that of the current instance, using the default comparer for that field's type.
  1419. /// </remarks>
  1420. public bool Equals(ValueTuple<T1, T2, T3, T4, T5, T6, T7> other)
  1421. {
  1422. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  1423. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  1424. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3)
  1425. && EqualityComparer<T4>.Default.Equals(Item4, other.Item4)
  1426. && EqualityComparer<T5>.Default.Equals(Item5, other.Item5)
  1427. && EqualityComparer<T6>.Default.Equals(Item6, other.Item6)
  1428. && EqualityComparer<T7>.Default.Equals(Item7, other.Item7);
  1429. }
  1430. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  1431. {
  1432. if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>)) return false;
  1433. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other;
  1434. return comparer.Equals(Item1, objTuple.Item1)
  1435. && comparer.Equals(Item2, objTuple.Item2)
  1436. && comparer.Equals(Item3, objTuple.Item3)
  1437. && comparer.Equals(Item4, objTuple.Item4)
  1438. && comparer.Equals(Item5, objTuple.Item5)
  1439. && comparer.Equals(Item6, objTuple.Item6)
  1440. && comparer.Equals(Item7, objTuple.Item7);
  1441. }
  1442. int IComparable.CompareTo(object? other)
  1443. {
  1444. if (other == null) return 1;
  1445. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
  1446. {
  1447. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1448. }
  1449. return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other);
  1450. }
  1451. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  1452. /// <param name="other">An instance to compare.</param>
  1453. /// <returns>
  1454. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  1455. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  1456. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  1457. /// than <paramref name="other"/>.
  1458. /// </returns>
  1459. public int CompareTo(ValueTuple<T1, T2, T3, T4, T5, T6, T7> other)
  1460. {
  1461. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  1462. if (c != 0) return c;
  1463. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  1464. if (c != 0) return c;
  1465. c = Comparer<T3>.Default.Compare(Item3, other.Item3);
  1466. if (c != 0) return c;
  1467. c = Comparer<T4>.Default.Compare(Item4, other.Item4);
  1468. if (c != 0) return c;
  1469. c = Comparer<T5>.Default.Compare(Item5, other.Item5);
  1470. if (c != 0) return c;
  1471. c = Comparer<T6>.Default.Compare(Item6, other.Item6);
  1472. if (c != 0) return c;
  1473. return Comparer<T7>.Default.Compare(Item7, other.Item7);
  1474. }
  1475. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  1476. {
  1477. if (other == null) return 1;
  1478. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
  1479. {
  1480. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1481. }
  1482. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other;
  1483. int c = comparer.Compare(Item1, objTuple.Item1);
  1484. if (c != 0) return c;
  1485. c = comparer.Compare(Item2, objTuple.Item2);
  1486. if (c != 0) return c;
  1487. c = comparer.Compare(Item3, objTuple.Item3);
  1488. if (c != 0) return c;
  1489. c = comparer.Compare(Item4, objTuple.Item4);
  1490. if (c != 0) return c;
  1491. c = comparer.Compare(Item5, objTuple.Item5);
  1492. if (c != 0) return c;
  1493. c = comparer.Compare(Item6, objTuple.Item6);
  1494. if (c != 0) return c;
  1495. return comparer.Compare(Item7, objTuple.Item7);
  1496. }
  1497. /// <summary>
  1498. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance.
  1499. /// </summary>
  1500. /// <returns>A 32-bit signed integer hash code.</returns>
  1501. public override int GetHashCode()
  1502. {
  1503. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  1504. Item2?.GetHashCode() ?? 0,
  1505. Item3?.GetHashCode() ?? 0,
  1506. Item4?.GetHashCode() ?? 0,
  1507. Item5?.GetHashCode() ?? 0,
  1508. Item6?.GetHashCode() ?? 0,
  1509. Item7?.GetHashCode() ?? 0);
  1510. }
  1511. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  1512. {
  1513. return GetHashCodeCore(comparer);
  1514. }
  1515. private int GetHashCodeCore(IEqualityComparer comparer)
  1516. {
  1517. return HashCode.Combine(comparer.GetHashCode(Item1!),
  1518. comparer.GetHashCode(Item2!),
  1519. comparer.GetHashCode(Item3!),
  1520. comparer.GetHashCode(Item4!),
  1521. comparer.GetHashCode(Item5!),
  1522. comparer.GetHashCode(Item6!),
  1523. comparer.GetHashCode(Item7!));
  1524. }
  1525. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  1526. {
  1527. return GetHashCodeCore(comparer);
  1528. }
  1529. /// <summary>
  1530. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance.
  1531. /// </summary>
  1532. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7}"/> instance.</returns>
  1533. /// <remarks>
  1534. /// The string returned by this method takes the form <c>(Item1, Item2, Item3, Item4, Item5, Item6, Item7)</c>.
  1535. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  1536. /// </remarks>
  1537. public override string ToString()
  1538. {
  1539. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ")";
  1540. }
  1541. string IValueTupleInternal.ToStringEnd()
  1542. {
  1543. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ")";
  1544. }
  1545. /// <summary>
  1546. /// The number of positions in this data structure.
  1547. /// </summary>
  1548. int ITuple.Length => 7;
  1549. /// <summary>
  1550. /// Get the element at position <param name="index"/>.
  1551. /// </summary>
  1552. object? ITuple.this[int index] =>
  1553. index switch
  1554. {
  1555. 0 => Item1,
  1556. 1 => Item2,
  1557. 2 => Item3,
  1558. 3 => Item4,
  1559. 4 => Item5,
  1560. 5 => Item6,
  1561. 6 => Item7,
  1562. _ => throw new IndexOutOfRangeException(),
  1563. };
  1564. }
  1565. /// <summary>
  1566. /// Represents an 8-tuple, or octuple, as a value type.
  1567. /// </summary>
  1568. /// <typeparam name="T1">The type of the tuple's first component.</typeparam>
  1569. /// <typeparam name="T2">The type of the tuple's second component.</typeparam>
  1570. /// <typeparam name="T3">The type of the tuple's third component.</typeparam>
  1571. /// <typeparam name="T4">The type of the tuple's fourth component.</typeparam>
  1572. /// <typeparam name="T5">The type of the tuple's fifth component.</typeparam>
  1573. /// <typeparam name="T6">The type of the tuple's sixth component.</typeparam>
  1574. /// <typeparam name="T7">The type of the tuple's seventh component.</typeparam>
  1575. /// <typeparam name="TRest">The type of the tuple's eighth component.</typeparam>
  1576. [Serializable]
  1577. [StructLayout(LayoutKind.Auto)]
  1578. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  1579. public struct ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>
  1580. : IEquatable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>>, IValueTupleInternal, ITuple
  1581. where TRest : struct
  1582. {
  1583. /// <summary>
  1584. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's first component.
  1585. /// </summary>
  1586. public T1 Item1;
  1587. /// <summary>
  1588. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's second component.
  1589. /// </summary>
  1590. public T2 Item2;
  1591. /// <summary>
  1592. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's third component.
  1593. /// </summary>
  1594. public T3 Item3;
  1595. /// <summary>
  1596. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's fourth component.
  1597. /// </summary>
  1598. public T4 Item4;
  1599. /// <summary>
  1600. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's fifth component.
  1601. /// </summary>
  1602. public T5 Item5;
  1603. /// <summary>
  1604. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's sixth component.
  1605. /// </summary>
  1606. public T6 Item6;
  1607. /// <summary>
  1608. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's seventh component.
  1609. /// </summary>
  1610. public T7 Item7;
  1611. /// <summary>
  1612. /// The current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance's eighth component.
  1613. /// </summary>
  1614. public TRest Rest;
  1615. /// <summary>
  1616. /// Initializes a new instance of the <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> value type.
  1617. /// </summary>
  1618. /// <param name="item1">The value of the tuple's first component.</param>
  1619. /// <param name="item2">The value of the tuple's second component.</param>
  1620. /// <param name="item3">The value of the tuple's third component.</param>
  1621. /// <param name="item4">The value of the tuple's fourth component.</param>
  1622. /// <param name="item5">The value of the tuple's fifth component.</param>
  1623. /// <param name="item6">The value of the tuple's sixth component.</param>
  1624. /// <param name="item7">The value of the tuple's seventh component.</param>
  1625. /// <param name="rest">The value of the tuple's eight component.</param>
  1626. public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest)
  1627. {
  1628. if (!(rest is IValueTupleInternal))
  1629. {
  1630. throw new ArgumentException(SR.ArgumentException_ValueTupleLastArgumentNotAValueTuple);
  1631. }
  1632. Item1 = item1;
  1633. Item2 = item2;
  1634. Item3 = item3;
  1635. Item4 = item4;
  1636. Item5 = item5;
  1637. Item6 = item6;
  1638. Item7 = item7;
  1639. Rest = rest;
  1640. }
  1641. /// <summary>
  1642. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance is equal to a specified object.
  1643. /// </summary>
  1644. /// <param name="obj">The object to compare with this instance.</param>
  1645. /// <returns><see langword="true"/> if the current instance is equal to the specified object; otherwise, <see langword="false"/>.</returns>
  1646. /// <remarks>
  1647. /// The <paramref name="obj"/> parameter is considered to be equal to the current instance under the following conditions:
  1648. /// <list type="bullet">
  1649. /// <item><description>It is a <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> value type.</description></item>
  1650. /// <item><description>Its components are of the same types as those of the current instance.</description></item>
  1651. /// <item><description>Its components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.</description></item>
  1652. /// </list>
  1653. /// </remarks>
  1654. public override bool Equals(object? obj)
  1655. {
  1656. return obj is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> && Equals((ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)obj);
  1657. }
  1658. /// <summary>
  1659. /// Returns a value that indicates whether the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/>
  1660. /// instance is equal to a specified <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/>.
  1661. /// </summary>
  1662. /// <param name="other">The tuple to compare with this instance.</param>
  1663. /// <returns><see langword="true"/> if the current instance is equal to the specified tuple; otherwise, <see langword="false"/>.</returns>
  1664. /// <remarks>
  1665. /// The <paramref name="other"/> parameter is considered to be equal to the current instance if each of its fields
  1666. /// are equal to that of the current instance, using the default comparer for that field's type.
  1667. /// </remarks>
  1668. public bool Equals(ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> other)
  1669. {
  1670. return EqualityComparer<T1>.Default.Equals(Item1, other.Item1)
  1671. && EqualityComparer<T2>.Default.Equals(Item2, other.Item2)
  1672. && EqualityComparer<T3>.Default.Equals(Item3, other.Item3)
  1673. && EqualityComparer<T4>.Default.Equals(Item4, other.Item4)
  1674. && EqualityComparer<T5>.Default.Equals(Item5, other.Item5)
  1675. && EqualityComparer<T6>.Default.Equals(Item6, other.Item6)
  1676. && EqualityComparer<T7>.Default.Equals(Item7, other.Item7)
  1677. && EqualityComparer<TRest>.Default.Equals(Rest, other.Rest);
  1678. }
  1679. bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer)
  1680. {
  1681. if (other == null || !(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)) return false;
  1682. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other;
  1683. return comparer.Equals(Item1, objTuple.Item1)
  1684. && comparer.Equals(Item2, objTuple.Item2)
  1685. && comparer.Equals(Item3, objTuple.Item3)
  1686. && comparer.Equals(Item4, objTuple.Item4)
  1687. && comparer.Equals(Item5, objTuple.Item5)
  1688. && comparer.Equals(Item6, objTuple.Item6)
  1689. && comparer.Equals(Item7, objTuple.Item7)
  1690. && comparer.Equals(Rest, objTuple.Rest);
  1691. }
  1692. int IComparable.CompareTo(object? other)
  1693. {
  1694. if (other == null) return 1;
  1695. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
  1696. {
  1697. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1698. }
  1699. return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other);
  1700. }
  1701. /// <summary>Compares this instance to a specified instance and returns an indication of their relative values.</summary>
  1702. /// <param name="other">An instance to compare.</param>
  1703. /// <returns>
  1704. /// A signed number indicating the relative values of this instance and <paramref name="other"/>.
  1705. /// Returns less than zero if this instance is less than <paramref name="other"/>, zero if this
  1706. /// instance is equal to <paramref name="other"/>, and greater than zero if this instance is greater
  1707. /// than <paramref name="other"/>.
  1708. /// </returns>
  1709. public int CompareTo(ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> other)
  1710. {
  1711. int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
  1712. if (c != 0) return c;
  1713. c = Comparer<T2>.Default.Compare(Item2, other.Item2);
  1714. if (c != 0) return c;
  1715. c = Comparer<T3>.Default.Compare(Item3, other.Item3);
  1716. if (c != 0) return c;
  1717. c = Comparer<T4>.Default.Compare(Item4, other.Item4);
  1718. if (c != 0) return c;
  1719. c = Comparer<T5>.Default.Compare(Item5, other.Item5);
  1720. if (c != 0) return c;
  1721. c = Comparer<T6>.Default.Compare(Item6, other.Item6);
  1722. if (c != 0) return c;
  1723. c = Comparer<T7>.Default.Compare(Item7, other.Item7);
  1724. if (c != 0) return c;
  1725. return Comparer<TRest>.Default.Compare(Rest, other.Rest);
  1726. }
  1727. int IStructuralComparable.CompareTo(object? other, IComparer comparer)
  1728. {
  1729. if (other == null) return 1;
  1730. if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
  1731. {
  1732. throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, GetType()), nameof(other));
  1733. }
  1734. var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other;
  1735. int c = comparer.Compare(Item1, objTuple.Item1);
  1736. if (c != 0) return c;
  1737. c = comparer.Compare(Item2, objTuple.Item2);
  1738. if (c != 0) return c;
  1739. c = comparer.Compare(Item3, objTuple.Item3);
  1740. if (c != 0) return c;
  1741. c = comparer.Compare(Item4, objTuple.Item4);
  1742. if (c != 0) return c;
  1743. c = comparer.Compare(Item5, objTuple.Item5);
  1744. if (c != 0) return c;
  1745. c = comparer.Compare(Item6, objTuple.Item6);
  1746. if (c != 0) return c;
  1747. c = comparer.Compare(Item7, objTuple.Item7);
  1748. if (c != 0) return c;
  1749. return comparer.Compare(Rest, objTuple.Rest);
  1750. }
  1751. /// <summary>
  1752. /// Returns the hash code for the current <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance.
  1753. /// </summary>
  1754. /// <returns>A 32-bit signed integer hash code.</returns>
  1755. public override int GetHashCode()
  1756. {
  1757. // We want to have a limited hash in this case. We'll use the first 7 elements of the tuple
  1758. if (!(Rest is IValueTupleInternal))
  1759. {
  1760. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  1761. Item2?.GetHashCode() ?? 0,
  1762. Item3?.GetHashCode() ?? 0,
  1763. Item4?.GetHashCode() ?? 0,
  1764. Item5?.GetHashCode() ?? 0,
  1765. Item6?.GetHashCode() ?? 0,
  1766. Item7?.GetHashCode() ?? 0);
  1767. }
  1768. int size = ((IValueTupleInternal)Rest).Length;
  1769. int restHashCode = Rest.GetHashCode();
  1770. if (size >= 8)
  1771. {
  1772. return restHashCode;
  1773. }
  1774. // In this case, the rest member has less than 8 elements so we need to combine some of our elements with the elements in rest
  1775. int k = 8 - size;
  1776. switch (k)
  1777. {
  1778. case 1:
  1779. return HashCode.Combine(Item7?.GetHashCode() ?? 0,
  1780. restHashCode);
  1781. case 2:
  1782. return HashCode.Combine(Item6?.GetHashCode() ?? 0,
  1783. Item7?.GetHashCode() ?? 0,
  1784. restHashCode);
  1785. case 3:
  1786. return HashCode.Combine(Item5?.GetHashCode() ?? 0,
  1787. Item6?.GetHashCode() ?? 0,
  1788. Item7?.GetHashCode() ?? 0,
  1789. restHashCode);
  1790. case 4:
  1791. return HashCode.Combine(Item4?.GetHashCode() ?? 0,
  1792. Item5?.GetHashCode() ?? 0,
  1793. Item6?.GetHashCode() ?? 0,
  1794. Item7?.GetHashCode() ?? 0,
  1795. restHashCode);
  1796. case 5:
  1797. return HashCode.Combine(Item3?.GetHashCode() ?? 0,
  1798. Item4?.GetHashCode() ?? 0,
  1799. Item5?.GetHashCode() ?? 0,
  1800. Item6?.GetHashCode() ?? 0,
  1801. Item7?.GetHashCode() ?? 0,
  1802. restHashCode);
  1803. case 6:
  1804. return HashCode.Combine(Item2?.GetHashCode() ?? 0,
  1805. Item3?.GetHashCode() ?? 0,
  1806. Item4?.GetHashCode() ?? 0,
  1807. Item5?.GetHashCode() ?? 0,
  1808. Item6?.GetHashCode() ?? 0,
  1809. Item7?.GetHashCode() ?? 0,
  1810. restHashCode);
  1811. case 7:
  1812. case 8:
  1813. return HashCode.Combine(Item1?.GetHashCode() ?? 0,
  1814. Item2?.GetHashCode() ?? 0,
  1815. Item3?.GetHashCode() ?? 0,
  1816. Item4?.GetHashCode() ?? 0,
  1817. Item5?.GetHashCode() ?? 0,
  1818. Item6?.GetHashCode() ?? 0,
  1819. Item7?.GetHashCode() ?? 0,
  1820. restHashCode);
  1821. }
  1822. Debug.Fail("Missed all cases for computing ValueTuple hash code");
  1823. return -1;
  1824. }
  1825. int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
  1826. {
  1827. return GetHashCodeCore(comparer);
  1828. }
  1829. private int GetHashCodeCore(IEqualityComparer comparer)
  1830. {
  1831. // We want to have a limited hash in this case. We'll use the first 7 elements of the tuple
  1832. if (!(Rest is IValueTupleInternal rest))
  1833. {
  1834. return HashCode.Combine(comparer.GetHashCode(Item1!), comparer.GetHashCode(Item2!), comparer.GetHashCode(Item3!),
  1835. comparer.GetHashCode(Item4!), comparer.GetHashCode(Item5!), comparer.GetHashCode(Item6!),
  1836. comparer.GetHashCode(Item7!));
  1837. }
  1838. int size = rest.Length;
  1839. int restHashCode = rest.GetHashCode(comparer);
  1840. if (size >= 8)
  1841. {
  1842. return restHashCode;
  1843. }
  1844. // In this case, the rest member has less than 8 elements so we need to combine some our elements with the elements in rest
  1845. int k = 8 - size;
  1846. switch (k)
  1847. {
  1848. case 1:
  1849. return HashCode.Combine(comparer.GetHashCode(Item7!),
  1850. restHashCode);
  1851. case 2:
  1852. return HashCode.Combine(comparer.GetHashCode(Item6!),
  1853. comparer.GetHashCode(Item7!),
  1854. restHashCode);
  1855. case 3:
  1856. return HashCode.Combine(comparer.GetHashCode(Item5!),
  1857. comparer.GetHashCode(Item6!),
  1858. comparer.GetHashCode(Item7!),
  1859. restHashCode);
  1860. case 4:
  1861. return HashCode.Combine(comparer.GetHashCode(Item4!),
  1862. comparer.GetHashCode(Item5!),
  1863. comparer.GetHashCode(Item6!),
  1864. comparer.GetHashCode(Item7!),
  1865. restHashCode);
  1866. case 5:
  1867. return HashCode.Combine(comparer.GetHashCode(Item3!),
  1868. comparer.GetHashCode(Item4!),
  1869. comparer.GetHashCode(Item5!),
  1870. comparer.GetHashCode(Item6!),
  1871. comparer.GetHashCode(Item7!),
  1872. restHashCode);
  1873. case 6:
  1874. return HashCode.Combine(comparer.GetHashCode(Item2!),
  1875. comparer.GetHashCode(Item3!),
  1876. comparer.GetHashCode(Item4!),
  1877. comparer.GetHashCode(Item5!),
  1878. comparer.GetHashCode(Item6!),
  1879. comparer.GetHashCode(Item7!),
  1880. restHashCode);
  1881. case 7:
  1882. case 8:
  1883. return HashCode.Combine(comparer.GetHashCode(Item1!),
  1884. comparer.GetHashCode(Item2!),
  1885. comparer.GetHashCode(Item3!),
  1886. comparer.GetHashCode(Item4!),
  1887. comparer.GetHashCode(Item5!),
  1888. comparer.GetHashCode(Item6!),
  1889. comparer.GetHashCode(Item7!),
  1890. restHashCode);
  1891. }
  1892. Debug.Fail("Missed all cases for computing ValueTuple hash code");
  1893. return -1;
  1894. }
  1895. int IValueTupleInternal.GetHashCode(IEqualityComparer comparer)
  1896. {
  1897. return GetHashCodeCore(comparer);
  1898. }
  1899. /// <summary>
  1900. /// Returns a string that represents the value of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance.
  1901. /// </summary>
  1902. /// <returns>The string representation of this <see cref="ValueTuple{T1, T2, T3, T4, T5, T6, T7, TRest}"/> instance.</returns>
  1903. /// <remarks>
  1904. /// The string returned by this method takes the form <c>(Item1, Item2, Item3, Item4, Item5, Item6, Item7, Rest)</c>.
  1905. /// If any field value is <see langword="null"/>, it is represented as <see cref="string.Empty"/>.
  1906. /// </remarks>
  1907. public override string ToString()
  1908. {
  1909. if (Rest is IValueTupleInternal)
  1910. {
  1911. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ", " + ((IValueTupleInternal)Rest).ToStringEnd();
  1912. }
  1913. return "(" + Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ", " + Rest.ToString() + ")";
  1914. }
  1915. string IValueTupleInternal.ToStringEnd()
  1916. {
  1917. if (Rest is IValueTupleInternal)
  1918. {
  1919. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ", " + ((IValueTupleInternal)Rest).ToStringEnd();
  1920. }
  1921. return Item1?.ToString() + ", " + Item2?.ToString() + ", " + Item3?.ToString() + ", " + Item4?.ToString() + ", " + Item5?.ToString() + ", " + Item6?.ToString() + ", " + Item7?.ToString() + ", " + Rest.ToString() + ")";
  1922. }
  1923. /// <summary>
  1924. /// The number of positions in this data structure.
  1925. /// </summary>
  1926. int ITuple.Length => Rest is IValueTupleInternal ? 7 + ((IValueTupleInternal)Rest).Length : 8;
  1927. /// <summary>
  1928. /// Get the element at position <param name="index"/>.
  1929. /// </summary>
  1930. object? ITuple.this[int index]
  1931. {
  1932. get
  1933. {
  1934. switch (index)
  1935. {
  1936. case 0:
  1937. return Item1;
  1938. case 1:
  1939. return Item2;
  1940. case 2:
  1941. return Item3;
  1942. case 3:
  1943. return Item4;
  1944. case 4:
  1945. return Item5;
  1946. case 5:
  1947. return Item6;
  1948. case 6:
  1949. return Item7;
  1950. }
  1951. if (Rest is IValueTupleInternal)
  1952. {
  1953. return ((IValueTupleInternal)Rest)[index - 7];
  1954. }
  1955. if (index == 7)
  1956. {
  1957. return Rest;
  1958. }
  1959. throw new IndexOutOfRangeException();
  1960. }
  1961. }
  1962. }
  1963. }