Min.Primitive.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. private static async Task<int> MinCore(IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  12. {
  13. int value;
  14. IAsyncEnumerator<int> e = source.GetAsyncEnumerator(cancellationToken);
  15. try
  16. {
  17. if (!await e.MoveNextAsync().ConfigureAwait(false))
  18. {
  19. throw Error.NoElements();
  20. }
  21. value = e.Current;
  22. while (await e.MoveNextAsync().ConfigureAwait(false))
  23. {
  24. int x = e.Current;
  25. if (x < value)
  26. {
  27. value = x;
  28. }
  29. }
  30. }
  31. finally
  32. {
  33. await e.DisposeAsync().ConfigureAwait(false);
  34. }
  35. return value;
  36. }
  37. private static async Task<int?> MinCore(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  38. {
  39. int? value = null;
  40. IAsyncEnumerator<int?> e = source.GetAsyncEnumerator(cancellationToken);
  41. try
  42. {
  43. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  44. // so we don't have to keep testing for nullity.
  45. do
  46. {
  47. if (!await e.MoveNextAsync().ConfigureAwait(false))
  48. {
  49. return value;
  50. }
  51. value = e.Current;
  52. }
  53. while (!value.HasValue);
  54. // Keep hold of the wrapped value, and do comparisons on that, rather than
  55. // using the lifted operation each time.
  56. int valueVal = value.GetValueOrDefault();
  57. while (await e.MoveNextAsync().ConfigureAwait(false))
  58. {
  59. int? cur = e.Current;
  60. int x = cur.GetValueOrDefault();
  61. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  62. // unless nulls either never happen or always happen.
  63. if (cur.HasValue & x < valueVal)
  64. {
  65. valueVal = x;
  66. value = cur;
  67. }
  68. }
  69. }
  70. finally
  71. {
  72. await e.DisposeAsync().ConfigureAwait(false);
  73. }
  74. return value;
  75. }
  76. private static async Task<long> MinCore(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  77. {
  78. long value;
  79. IAsyncEnumerator<long> e = source.GetAsyncEnumerator(cancellationToken);
  80. try
  81. {
  82. if (!await e.MoveNextAsync().ConfigureAwait(false))
  83. {
  84. throw Error.NoElements();
  85. }
  86. value = e.Current;
  87. while (await e.MoveNextAsync().ConfigureAwait(false))
  88. {
  89. long x = e.Current;
  90. if (x < value)
  91. {
  92. value = x;
  93. }
  94. }
  95. }
  96. finally
  97. {
  98. await e.DisposeAsync().ConfigureAwait(false);
  99. }
  100. return value;
  101. }
  102. private static async Task<long?> MinCore(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  103. {
  104. long? value = null;
  105. IAsyncEnumerator<long?> e = source.GetAsyncEnumerator(cancellationToken);
  106. try
  107. {
  108. do
  109. {
  110. if (!await e.MoveNextAsync().ConfigureAwait(false))
  111. {
  112. return value;
  113. }
  114. value = e.Current;
  115. }
  116. while (!value.HasValue);
  117. long valueVal = value.GetValueOrDefault();
  118. while (await e.MoveNextAsync().ConfigureAwait(false))
  119. {
  120. long? cur = e.Current;
  121. long x = cur.GetValueOrDefault();
  122. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  123. // unless nulls either never happen or always happen.
  124. if (cur.HasValue & x < valueVal)
  125. {
  126. valueVal = x;
  127. value = cur;
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. await e.DisposeAsync().ConfigureAwait(false);
  134. }
  135. return value;
  136. }
  137. private static async Task<float> MinCore(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  138. {
  139. float value;
  140. IAsyncEnumerator<float> e = source.GetAsyncEnumerator(cancellationToken);
  141. try
  142. {
  143. if (!await e.MoveNextAsync().ConfigureAwait(false))
  144. {
  145. throw Error.NoElements();
  146. }
  147. value = e.Current;
  148. while (await e.MoveNextAsync().ConfigureAwait(false))
  149. {
  150. float x = e.Current;
  151. if (x < value)
  152. {
  153. value = x;
  154. }
  155. else if (float.IsNaN(x))
  156. {
  157. // Normally NaN < anything is false, as is anything < NaN
  158. // However, this leads to some irksome outcomes in Min and Max.
  159. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  160. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  161. // ordering where NaN is smaller than every value, including
  162. // negative infinity.
  163. // Not testing for NaN therefore isn't an option, but since we
  164. // can't find a smaller value, we can short-circuit.
  165. return x;
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. await e.DisposeAsync().ConfigureAwait(false);
  172. }
  173. return value;
  174. }
  175. private static async Task<float?> MinCore(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  176. {
  177. float? value = null;
  178. IAsyncEnumerator<float?> e = source.GetAsyncEnumerator(cancellationToken);
  179. try
  180. {
  181. do
  182. {
  183. if (!await e.MoveNextAsync().ConfigureAwait(false))
  184. {
  185. return value;
  186. }
  187. value = e.Current;
  188. }
  189. while (!value.HasValue);
  190. float valueVal = value.GetValueOrDefault();
  191. while (await e.MoveNextAsync().ConfigureAwait(false))
  192. {
  193. float? cur = e.Current;
  194. if (cur.HasValue)
  195. {
  196. float x = cur.GetValueOrDefault();
  197. if (x < valueVal)
  198. {
  199. valueVal = x;
  200. value = cur;
  201. }
  202. else if (float.IsNaN(x))
  203. {
  204. return cur;
  205. }
  206. }
  207. }
  208. }
  209. finally
  210. {
  211. await e.DisposeAsync().ConfigureAwait(false);
  212. }
  213. return value;
  214. }
  215. private static async Task<double> MinCore(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  216. {
  217. double value;
  218. IAsyncEnumerator<double> e = source.GetAsyncEnumerator(cancellationToken);
  219. try
  220. {
  221. if (!await e.MoveNextAsync().ConfigureAwait(false))
  222. {
  223. throw Error.NoElements();
  224. }
  225. value = e.Current;
  226. while (await e.MoveNextAsync().ConfigureAwait(false))
  227. {
  228. double x = e.Current;
  229. if (x < value)
  230. {
  231. value = x;
  232. }
  233. else if (double.IsNaN(x))
  234. {
  235. return x;
  236. }
  237. }
  238. }
  239. finally
  240. {
  241. await e.DisposeAsync().ConfigureAwait(false);
  242. }
  243. return value;
  244. }
  245. private static async Task<double?> MinCore(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  246. {
  247. double? value = null;
  248. IAsyncEnumerator<double?> e = source.GetAsyncEnumerator(cancellationToken);
  249. try
  250. {
  251. do
  252. {
  253. if (!await e.MoveNextAsync().ConfigureAwait(false))
  254. {
  255. return value;
  256. }
  257. value = e.Current;
  258. }
  259. while (!value.HasValue);
  260. double valueVal = value.GetValueOrDefault();
  261. while (await e.MoveNextAsync().ConfigureAwait(false))
  262. {
  263. double? cur = e.Current;
  264. if (cur.HasValue)
  265. {
  266. double x = cur.GetValueOrDefault();
  267. if (x < valueVal)
  268. {
  269. valueVal = x;
  270. value = cur;
  271. }
  272. else if (double.IsNaN(x))
  273. {
  274. return cur;
  275. }
  276. }
  277. }
  278. }
  279. finally
  280. {
  281. await e.DisposeAsync().ConfigureAwait(false);
  282. }
  283. return value;
  284. }
  285. private static async Task<decimal> MinCore(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  286. {
  287. decimal value;
  288. IAsyncEnumerator<decimal> e = source.GetAsyncEnumerator(cancellationToken);
  289. try
  290. {
  291. if (!await e.MoveNextAsync().ConfigureAwait(false))
  292. {
  293. throw Error.NoElements();
  294. }
  295. value = e.Current;
  296. while (await e.MoveNextAsync().ConfigureAwait(false))
  297. {
  298. decimal x = e.Current;
  299. if (x < value)
  300. {
  301. value = x;
  302. }
  303. }
  304. }
  305. finally
  306. {
  307. await e.DisposeAsync().ConfigureAwait(false);
  308. }
  309. return value;
  310. }
  311. private static async Task<decimal?> MinCore(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  312. {
  313. decimal? value = null;
  314. IAsyncEnumerator<decimal?> e = source.GetAsyncEnumerator(cancellationToken);
  315. try
  316. {
  317. do
  318. {
  319. if (!await e.MoveNextAsync().ConfigureAwait(false))
  320. {
  321. return value;
  322. }
  323. value = e.Current;
  324. }
  325. while (!value.HasValue);
  326. decimal valueVal = value.GetValueOrDefault();
  327. while (await e.MoveNextAsync().ConfigureAwait(false))
  328. {
  329. decimal? cur = e.Current;
  330. decimal x = cur.GetValueOrDefault();
  331. if (cur.HasValue && x < valueVal)
  332. {
  333. valueVal = x;
  334. value = cur;
  335. }
  336. }
  337. }
  338. finally
  339. {
  340. await e.DisposeAsync().ConfigureAwait(false);
  341. }
  342. return value;
  343. }
  344. private static async Task<int> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  345. {
  346. int value;
  347. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  348. try
  349. {
  350. if (!await e.MoveNextAsync().ConfigureAwait(false))
  351. {
  352. throw Error.NoElements();
  353. }
  354. value = selector(e.Current);
  355. while (await e.MoveNextAsync().ConfigureAwait(false))
  356. {
  357. int x = selector(e.Current);
  358. if (x < value)
  359. {
  360. value = x;
  361. }
  362. }
  363. }
  364. finally
  365. {
  366. await e.DisposeAsync().ConfigureAwait(false);
  367. }
  368. return value;
  369. }
  370. private static async Task<int?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  371. {
  372. int? value = null;
  373. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  374. try
  375. {
  376. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  377. // so we don't have to keep testing for nullity.
  378. do
  379. {
  380. if (!await e.MoveNextAsync().ConfigureAwait(false))
  381. {
  382. return value;
  383. }
  384. value = selector(e.Current);
  385. }
  386. while (!value.HasValue);
  387. // Keep hold of the wrapped value, and do comparisons on that, rather than
  388. // using the lifted operation each time.
  389. int valueVal = value.GetValueOrDefault();
  390. while (await e.MoveNextAsync().ConfigureAwait(false))
  391. {
  392. int? cur = selector(e.Current);
  393. int x = cur.GetValueOrDefault();
  394. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  395. // unless nulls either never happen or always happen.
  396. if (cur.HasValue & x < valueVal)
  397. {
  398. valueVal = x;
  399. value = cur;
  400. }
  401. }
  402. }
  403. finally
  404. {
  405. await e.DisposeAsync().ConfigureAwait(false);
  406. }
  407. return value;
  408. }
  409. private static async Task<long> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  410. {
  411. long value;
  412. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  413. try
  414. {
  415. if (!await e.MoveNextAsync().ConfigureAwait(false))
  416. {
  417. throw Error.NoElements();
  418. }
  419. value = selector(e.Current);
  420. while (await e.MoveNextAsync().ConfigureAwait(false))
  421. {
  422. long x = selector(e.Current);
  423. if (x < value)
  424. {
  425. value = x;
  426. }
  427. }
  428. }
  429. finally
  430. {
  431. await e.DisposeAsync().ConfigureAwait(false);
  432. }
  433. return value;
  434. }
  435. private static async Task<long?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  436. {
  437. long? value = null;
  438. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  439. try
  440. {
  441. do
  442. {
  443. if (!await e.MoveNextAsync().ConfigureAwait(false))
  444. {
  445. return value;
  446. }
  447. value = selector(e.Current);
  448. }
  449. while (!value.HasValue);
  450. long valueVal = value.GetValueOrDefault();
  451. while (await e.MoveNextAsync().ConfigureAwait(false))
  452. {
  453. long? cur = selector(e.Current);
  454. long x = cur.GetValueOrDefault();
  455. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  456. // unless nulls either never happen or always happen.
  457. if (cur.HasValue & x < valueVal)
  458. {
  459. valueVal = x;
  460. value = cur;
  461. }
  462. }
  463. }
  464. finally
  465. {
  466. await e.DisposeAsync().ConfigureAwait(false);
  467. }
  468. return value;
  469. }
  470. private static async Task<float> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  471. {
  472. float value;
  473. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  474. try
  475. {
  476. if (!await e.MoveNextAsync().ConfigureAwait(false))
  477. {
  478. throw Error.NoElements();
  479. }
  480. value = selector(e.Current);
  481. while (await e.MoveNextAsync().ConfigureAwait(false))
  482. {
  483. float x = selector(e.Current);
  484. if (x < value)
  485. {
  486. value = x;
  487. }
  488. else if (float.IsNaN(x))
  489. {
  490. // Normally NaN < anything is false, as is anything < NaN
  491. // However, this leads to some irksome outcomes in Min and Max.
  492. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  493. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  494. // ordering where NaN is smaller than every value, including
  495. // negative infinity.
  496. // Not testing for NaN therefore isn't an option, but since we
  497. // can't find a smaller value, we can short-circuit.
  498. return x;
  499. }
  500. }
  501. }
  502. finally
  503. {
  504. await e.DisposeAsync().ConfigureAwait(false);
  505. }
  506. return value;
  507. }
  508. private static async Task<float?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  509. {
  510. float? value = null;
  511. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  512. try
  513. {
  514. do
  515. {
  516. if (!await e.MoveNextAsync().ConfigureAwait(false))
  517. {
  518. return value;
  519. }
  520. value = selector(e.Current);
  521. }
  522. while (!value.HasValue);
  523. float valueVal = value.GetValueOrDefault();
  524. while (await e.MoveNextAsync().ConfigureAwait(false))
  525. {
  526. float? cur = selector(e.Current);
  527. if (cur.HasValue)
  528. {
  529. float x = cur.GetValueOrDefault();
  530. if (x < valueVal)
  531. {
  532. valueVal = x;
  533. value = cur;
  534. }
  535. else if (float.IsNaN(x))
  536. {
  537. return cur;
  538. }
  539. }
  540. }
  541. }
  542. finally
  543. {
  544. await e.DisposeAsync().ConfigureAwait(false);
  545. }
  546. return value;
  547. }
  548. private static async Task<double> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  549. {
  550. double value;
  551. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  552. try
  553. {
  554. if (!await e.MoveNextAsync().ConfigureAwait(false))
  555. {
  556. throw Error.NoElements();
  557. }
  558. value = selector(e.Current);
  559. while (await e.MoveNextAsync().ConfigureAwait(false))
  560. {
  561. double x = selector(e.Current);
  562. if (x < value)
  563. {
  564. value = x;
  565. }
  566. else if (double.IsNaN(x))
  567. {
  568. return x;
  569. }
  570. }
  571. }
  572. finally
  573. {
  574. await e.DisposeAsync().ConfigureAwait(false);
  575. }
  576. return value;
  577. }
  578. private static async Task<double?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  579. {
  580. double? value = null;
  581. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  582. try
  583. {
  584. do
  585. {
  586. if (!await e.MoveNextAsync().ConfigureAwait(false))
  587. {
  588. return value;
  589. }
  590. value = selector(e.Current);
  591. }
  592. while (!value.HasValue);
  593. double valueVal = value.GetValueOrDefault();
  594. while (await e.MoveNextAsync().ConfigureAwait(false))
  595. {
  596. double? cur = selector(e.Current);
  597. if (cur.HasValue)
  598. {
  599. double x = cur.GetValueOrDefault();
  600. if (x < valueVal)
  601. {
  602. valueVal = x;
  603. value = cur;
  604. }
  605. else if (double.IsNaN(x))
  606. {
  607. return cur;
  608. }
  609. }
  610. }
  611. }
  612. finally
  613. {
  614. await e.DisposeAsync().ConfigureAwait(false);
  615. }
  616. return value;
  617. }
  618. private static async Task<decimal> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  619. {
  620. decimal value;
  621. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  622. try
  623. {
  624. if (!await e.MoveNextAsync().ConfigureAwait(false))
  625. {
  626. throw Error.NoElements();
  627. }
  628. value = selector(e.Current);
  629. while (await e.MoveNextAsync().ConfigureAwait(false))
  630. {
  631. decimal x = selector(e.Current);
  632. if (x < value)
  633. {
  634. value = x;
  635. }
  636. }
  637. }
  638. finally
  639. {
  640. await e.DisposeAsync().ConfigureAwait(false);
  641. }
  642. return value;
  643. }
  644. private static async Task<decimal?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  645. {
  646. decimal? value = null;
  647. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  648. try
  649. {
  650. do
  651. {
  652. if (!await e.MoveNextAsync().ConfigureAwait(false))
  653. {
  654. return value;
  655. }
  656. value = selector(e.Current);
  657. }
  658. while (!value.HasValue);
  659. decimal valueVal = value.GetValueOrDefault();
  660. while (await e.MoveNextAsync().ConfigureAwait(false))
  661. {
  662. decimal? cur = selector(e.Current);
  663. decimal x = cur.GetValueOrDefault();
  664. if (cur.HasValue && x < valueVal)
  665. {
  666. valueVal = x;
  667. value = cur;
  668. }
  669. }
  670. }
  671. finally
  672. {
  673. await e.DisposeAsync().ConfigureAwait(false);
  674. }
  675. return value;
  676. }
  677. private static async Task<int> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  678. {
  679. int value;
  680. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  681. try
  682. {
  683. if (!await e.MoveNextAsync().ConfigureAwait(false))
  684. {
  685. throw Error.NoElements();
  686. }
  687. value = await selector(e.Current).ConfigureAwait(false);
  688. while (await e.MoveNextAsync().ConfigureAwait(false))
  689. {
  690. int x = await selector(e.Current).ConfigureAwait(false);
  691. if (x < value)
  692. {
  693. value = x;
  694. }
  695. }
  696. }
  697. finally
  698. {
  699. await e.DisposeAsync().ConfigureAwait(false);
  700. }
  701. return value;
  702. }
  703. private static async Task<int?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  704. {
  705. int? value = null;
  706. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  707. try
  708. {
  709. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  710. // so we don't have to keep testing for nullity.
  711. do
  712. {
  713. if (!await e.MoveNextAsync().ConfigureAwait(false))
  714. {
  715. return value;
  716. }
  717. value = await selector(e.Current).ConfigureAwait(false);
  718. }
  719. while (!value.HasValue);
  720. // Keep hold of the wrapped value, and do comparisons on that, rather than
  721. // using the lifted operation each time.
  722. int valueVal = value.GetValueOrDefault();
  723. while (await e.MoveNextAsync().ConfigureAwait(false))
  724. {
  725. int? cur = await selector(e.Current).ConfigureAwait(false);
  726. int x = cur.GetValueOrDefault();
  727. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  728. // unless nulls either never happen or always happen.
  729. if (cur.HasValue & x < valueVal)
  730. {
  731. valueVal = x;
  732. value = cur;
  733. }
  734. }
  735. }
  736. finally
  737. {
  738. await e.DisposeAsync().ConfigureAwait(false);
  739. }
  740. return value;
  741. }
  742. private static async Task<long> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  743. {
  744. long value;
  745. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  746. try
  747. {
  748. if (!await e.MoveNextAsync().ConfigureAwait(false))
  749. {
  750. throw Error.NoElements();
  751. }
  752. value = await selector(e.Current).ConfigureAwait(false);
  753. while (await e.MoveNextAsync().ConfigureAwait(false))
  754. {
  755. long x = await selector(e.Current).ConfigureAwait(false);
  756. if (x < value)
  757. {
  758. value = x;
  759. }
  760. }
  761. }
  762. finally
  763. {
  764. await e.DisposeAsync().ConfigureAwait(false);
  765. }
  766. return value;
  767. }
  768. private static async Task<long?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  769. {
  770. long? value = null;
  771. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  772. try
  773. {
  774. do
  775. {
  776. if (!await e.MoveNextAsync().ConfigureAwait(false))
  777. {
  778. return value;
  779. }
  780. value = await selector(e.Current).ConfigureAwait(false);
  781. }
  782. while (!value.HasValue);
  783. long valueVal = value.GetValueOrDefault();
  784. while (await e.MoveNextAsync().ConfigureAwait(false))
  785. {
  786. long? cur = await selector(e.Current).ConfigureAwait(false);
  787. long x = cur.GetValueOrDefault();
  788. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  789. // unless nulls either never happen or always happen.
  790. if (cur.HasValue & x < valueVal)
  791. {
  792. valueVal = x;
  793. value = cur;
  794. }
  795. }
  796. }
  797. finally
  798. {
  799. await e.DisposeAsync().ConfigureAwait(false);
  800. }
  801. return value;
  802. }
  803. private static async Task<float> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  804. {
  805. float value;
  806. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  807. try
  808. {
  809. if (!await e.MoveNextAsync().ConfigureAwait(false))
  810. {
  811. throw Error.NoElements();
  812. }
  813. value = await selector(e.Current).ConfigureAwait(false);
  814. while (await e.MoveNextAsync().ConfigureAwait(false))
  815. {
  816. float x = await selector(e.Current).ConfigureAwait(false);
  817. if (x < value)
  818. {
  819. value = x;
  820. }
  821. else if (float.IsNaN(x))
  822. {
  823. // Normally NaN < anything is false, as is anything < NaN
  824. // However, this leads to some irksome outcomes in Min and Max.
  825. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  826. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  827. // ordering where NaN is smaller than every value, including
  828. // negative infinity.
  829. // Not testing for NaN therefore isn't an option, but since we
  830. // can't find a smaller value, we can short-circuit.
  831. return x;
  832. }
  833. }
  834. }
  835. finally
  836. {
  837. await e.DisposeAsync().ConfigureAwait(false);
  838. }
  839. return value;
  840. }
  841. private static async Task<float?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  842. {
  843. float? value = null;
  844. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  845. try
  846. {
  847. do
  848. {
  849. if (!await e.MoveNextAsync().ConfigureAwait(false))
  850. {
  851. return value;
  852. }
  853. value = await selector(e.Current).ConfigureAwait(false);
  854. }
  855. while (!value.HasValue);
  856. float valueVal = value.GetValueOrDefault();
  857. while (await e.MoveNextAsync().ConfigureAwait(false))
  858. {
  859. float? cur = await selector(e.Current).ConfigureAwait(false);
  860. if (cur.HasValue)
  861. {
  862. float x = cur.GetValueOrDefault();
  863. if (x < valueVal)
  864. {
  865. valueVal = x;
  866. value = cur;
  867. }
  868. else if (float.IsNaN(x))
  869. {
  870. return cur;
  871. }
  872. }
  873. }
  874. }
  875. finally
  876. {
  877. await e.DisposeAsync().ConfigureAwait(false);
  878. }
  879. return value;
  880. }
  881. private static async Task<double> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  882. {
  883. double value;
  884. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  885. try
  886. {
  887. if (!await e.MoveNextAsync().ConfigureAwait(false))
  888. {
  889. throw Error.NoElements();
  890. }
  891. value = await selector(e.Current).ConfigureAwait(false);
  892. while (await e.MoveNextAsync().ConfigureAwait(false))
  893. {
  894. double x = await selector(e.Current).ConfigureAwait(false);
  895. if (x < value)
  896. {
  897. value = x;
  898. }
  899. else if (double.IsNaN(x))
  900. {
  901. return x;
  902. }
  903. }
  904. }
  905. finally
  906. {
  907. await e.DisposeAsync().ConfigureAwait(false);
  908. }
  909. return value;
  910. }
  911. private static async Task<double?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  912. {
  913. double? value = null;
  914. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  915. try
  916. {
  917. do
  918. {
  919. if (!await e.MoveNextAsync().ConfigureAwait(false))
  920. {
  921. return value;
  922. }
  923. value = await selector(e.Current).ConfigureAwait(false);
  924. }
  925. while (!value.HasValue);
  926. double valueVal = value.GetValueOrDefault();
  927. while (await e.MoveNextAsync().ConfigureAwait(false))
  928. {
  929. double? cur = await selector(e.Current).ConfigureAwait(false);
  930. if (cur.HasValue)
  931. {
  932. double x = cur.GetValueOrDefault();
  933. if (x < valueVal)
  934. {
  935. valueVal = x;
  936. value = cur;
  937. }
  938. else if (double.IsNaN(x))
  939. {
  940. return cur;
  941. }
  942. }
  943. }
  944. }
  945. finally
  946. {
  947. await e.DisposeAsync().ConfigureAwait(false);
  948. }
  949. return value;
  950. }
  951. private static async Task<decimal> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  952. {
  953. decimal value;
  954. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  955. try
  956. {
  957. if (!await e.MoveNextAsync().ConfigureAwait(false))
  958. {
  959. throw Error.NoElements();
  960. }
  961. value = await selector(e.Current).ConfigureAwait(false);
  962. while (await e.MoveNextAsync().ConfigureAwait(false))
  963. {
  964. decimal x = await selector(e.Current).ConfigureAwait(false);
  965. if (x < value)
  966. {
  967. value = x;
  968. }
  969. }
  970. }
  971. finally
  972. {
  973. await e.DisposeAsync().ConfigureAwait(false);
  974. }
  975. return value;
  976. }
  977. private static async Task<decimal?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  978. {
  979. decimal? value = null;
  980. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  981. try
  982. {
  983. do
  984. {
  985. if (!await e.MoveNextAsync().ConfigureAwait(false))
  986. {
  987. return value;
  988. }
  989. value = await selector(e.Current).ConfigureAwait(false);
  990. }
  991. while (!value.HasValue);
  992. decimal valueVal = value.GetValueOrDefault();
  993. while (await e.MoveNextAsync().ConfigureAwait(false))
  994. {
  995. decimal? cur = await selector(e.Current).ConfigureAwait(false);
  996. decimal x = cur.GetValueOrDefault();
  997. if (cur.HasValue && x < valueVal)
  998. {
  999. valueVal = x;
  1000. value = cur;
  1001. }
  1002. }
  1003. }
  1004. finally
  1005. {
  1006. await e.DisposeAsync().ConfigureAwait(false);
  1007. }
  1008. return value;
  1009. }
  1010. #if !NO_DEEP_CANCELLATION
  1011. private static async Task<int> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  1012. {
  1013. int value;
  1014. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1015. try
  1016. {
  1017. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1018. {
  1019. throw Error.NoElements();
  1020. }
  1021. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1022. while (await e.MoveNextAsync().ConfigureAwait(false))
  1023. {
  1024. int x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1025. if (x < value)
  1026. {
  1027. value = x;
  1028. }
  1029. }
  1030. }
  1031. finally
  1032. {
  1033. await e.DisposeAsync().ConfigureAwait(false);
  1034. }
  1035. return value;
  1036. }
  1037. private static async Task<int?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  1038. {
  1039. int? value = null;
  1040. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1041. try
  1042. {
  1043. // Start off knowing that we've a non-null value (or exit here, knowing we don't)
  1044. // so we don't have to keep testing for nullity.
  1045. do
  1046. {
  1047. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1048. {
  1049. return value;
  1050. }
  1051. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1052. }
  1053. while (!value.HasValue);
  1054. // Keep hold of the wrapped value, and do comparisons on that, rather than
  1055. // using the lifted operation each time.
  1056. int valueVal = value.GetValueOrDefault();
  1057. while (await e.MoveNextAsync().ConfigureAwait(false))
  1058. {
  1059. int? cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1060. int x = cur.GetValueOrDefault();
  1061. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1062. // unless nulls either never happen or always happen.
  1063. if (cur.HasValue & x < valueVal)
  1064. {
  1065. valueVal = x;
  1066. value = cur;
  1067. }
  1068. }
  1069. }
  1070. finally
  1071. {
  1072. await e.DisposeAsync().ConfigureAwait(false);
  1073. }
  1074. return value;
  1075. }
  1076. private static async Task<long> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  1077. {
  1078. long value;
  1079. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1080. try
  1081. {
  1082. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1083. {
  1084. throw Error.NoElements();
  1085. }
  1086. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1087. while (await e.MoveNextAsync().ConfigureAwait(false))
  1088. {
  1089. long x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1090. if (x < value)
  1091. {
  1092. value = x;
  1093. }
  1094. }
  1095. }
  1096. finally
  1097. {
  1098. await e.DisposeAsync().ConfigureAwait(false);
  1099. }
  1100. return value;
  1101. }
  1102. private static async Task<long?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  1103. {
  1104. long? value = null;
  1105. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1106. try
  1107. {
  1108. do
  1109. {
  1110. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1111. {
  1112. return value;
  1113. }
  1114. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1115. }
  1116. while (!value.HasValue);
  1117. long valueVal = value.GetValueOrDefault();
  1118. while (await e.MoveNextAsync().ConfigureAwait(false))
  1119. {
  1120. long? cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1121. long x = cur.GetValueOrDefault();
  1122. // Do not replace & with &&. The branch prediction cost outweighs the extra operation
  1123. // unless nulls either never happen or always happen.
  1124. if (cur.HasValue & x < valueVal)
  1125. {
  1126. valueVal = x;
  1127. value = cur;
  1128. }
  1129. }
  1130. }
  1131. finally
  1132. {
  1133. await e.DisposeAsync().ConfigureAwait(false);
  1134. }
  1135. return value;
  1136. }
  1137. private static async Task<float> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  1138. {
  1139. float value;
  1140. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1141. try
  1142. {
  1143. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1144. {
  1145. throw Error.NoElements();
  1146. }
  1147. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1148. while (await e.MoveNextAsync().ConfigureAwait(false))
  1149. {
  1150. float x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1151. if (x < value)
  1152. {
  1153. value = x;
  1154. }
  1155. else if (float.IsNaN(x))
  1156. {
  1157. // Normally NaN < anything is false, as is anything < NaN
  1158. // However, this leads to some irksome outcomes in Min and Max.
  1159. // If we use those semantics then Min(NaN, 5.0) is NaN, but
  1160. // Min(5.0, NaN) is 5.0! To fix this, we impose a total
  1161. // ordering where NaN is smaller than every value, including
  1162. // negative infinity.
  1163. // Not testing for NaN therefore isn't an option, but since we
  1164. // can't find a smaller value, we can short-circuit.
  1165. return x;
  1166. }
  1167. }
  1168. }
  1169. finally
  1170. {
  1171. await e.DisposeAsync().ConfigureAwait(false);
  1172. }
  1173. return value;
  1174. }
  1175. private static async Task<float?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  1176. {
  1177. float? value = null;
  1178. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1179. try
  1180. {
  1181. do
  1182. {
  1183. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1184. {
  1185. return value;
  1186. }
  1187. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1188. }
  1189. while (!value.HasValue);
  1190. float valueVal = value.GetValueOrDefault();
  1191. while (await e.MoveNextAsync().ConfigureAwait(false))
  1192. {
  1193. float? cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1194. if (cur.HasValue)
  1195. {
  1196. float x = cur.GetValueOrDefault();
  1197. if (x < valueVal)
  1198. {
  1199. valueVal = x;
  1200. value = cur;
  1201. }
  1202. else if (float.IsNaN(x))
  1203. {
  1204. return cur;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. finally
  1210. {
  1211. await e.DisposeAsync().ConfigureAwait(false);
  1212. }
  1213. return value;
  1214. }
  1215. private static async Task<double> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  1216. {
  1217. double value;
  1218. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1219. try
  1220. {
  1221. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1222. {
  1223. throw Error.NoElements();
  1224. }
  1225. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1226. while (await e.MoveNextAsync().ConfigureAwait(false))
  1227. {
  1228. double x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1229. if (x < value)
  1230. {
  1231. value = x;
  1232. }
  1233. else if (double.IsNaN(x))
  1234. {
  1235. return x;
  1236. }
  1237. }
  1238. }
  1239. finally
  1240. {
  1241. await e.DisposeAsync().ConfigureAwait(false);
  1242. }
  1243. return value;
  1244. }
  1245. private static async Task<double?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  1246. {
  1247. double? value = null;
  1248. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1249. try
  1250. {
  1251. do
  1252. {
  1253. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1254. {
  1255. return value;
  1256. }
  1257. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1258. }
  1259. while (!value.HasValue);
  1260. double valueVal = value.GetValueOrDefault();
  1261. while (await e.MoveNextAsync().ConfigureAwait(false))
  1262. {
  1263. double? cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1264. if (cur.HasValue)
  1265. {
  1266. double x = cur.GetValueOrDefault();
  1267. if (x < valueVal)
  1268. {
  1269. valueVal = x;
  1270. value = cur;
  1271. }
  1272. else if (double.IsNaN(x))
  1273. {
  1274. return cur;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. finally
  1280. {
  1281. await e.DisposeAsync().ConfigureAwait(false);
  1282. }
  1283. return value;
  1284. }
  1285. private static async Task<decimal> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  1286. {
  1287. decimal value;
  1288. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1289. try
  1290. {
  1291. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1292. {
  1293. throw Error.NoElements();
  1294. }
  1295. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1296. while (await e.MoveNextAsync().ConfigureAwait(false))
  1297. {
  1298. decimal x = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1299. if (x < value)
  1300. {
  1301. value = x;
  1302. }
  1303. }
  1304. }
  1305. finally
  1306. {
  1307. await e.DisposeAsync().ConfigureAwait(false);
  1308. }
  1309. return value;
  1310. }
  1311. private static async Task<decimal?> MinCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1312. {
  1313. decimal? value = null;
  1314. IAsyncEnumerator<TSource> e = source.GetAsyncEnumerator(cancellationToken);
  1315. try
  1316. {
  1317. do
  1318. {
  1319. if (!await e.MoveNextAsync().ConfigureAwait(false))
  1320. {
  1321. return value;
  1322. }
  1323. value = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1324. }
  1325. while (!value.HasValue);
  1326. decimal valueVal = value.GetValueOrDefault();
  1327. while (await e.MoveNextAsync().ConfigureAwait(false))
  1328. {
  1329. decimal? cur = await selector(e.Current, cancellationToken).ConfigureAwait(false);
  1330. decimal x = cur.GetValueOrDefault();
  1331. if (cur.HasValue && x < valueVal)
  1332. {
  1333. valueVal = x;
  1334. value = cur;
  1335. }
  1336. }
  1337. }
  1338. finally
  1339. {
  1340. await e.DisposeAsync().ConfigureAwait(false);
  1341. }
  1342. return value;
  1343. }
  1344. #endif
  1345. }
  1346. }