2
0

ValueTuple.cs 109 KB

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