From 43883ee88f96c1d47f7f67ccab4d1c46fbb02e7f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sun, 14 Feb 2021 22:42:21 -0500 Subject: [PATCH] Follow-up to Enumerable.Min/Max Comparer.Default optimization I didn't notice the same pattern was employed in the `Func` overloads, so changing them, too. --- src/libraries/System.Linq/src/System/Linq/Max.cs | 13 +++++-------- src/libraries/System.Linq/src/System/Linq/Min.cs | 13 +++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Linq/src/System/Linq/Max.cs b/src/libraries/System.Linq/src/System/Linq/Max.cs index 0e9c4c0476ae0c..c430306156400c 100644 --- a/src/libraries/System.Linq/src/System/Linq/Max.cs +++ b/src/libraries/System.Linq/src/System/Linq/Max.cs @@ -991,11 +991,10 @@ public static decimal Max(this IEnumerable source, Func comparer = Comparer.Default; TResult? value = default; - if (value == null) + using (IEnumerator e = source.GetEnumerator()) { - using (IEnumerator e = source.GetEnumerator()) + if (value == null) { do { @@ -1008,6 +1007,7 @@ public static decimal Max(this IEnumerable source, Func comparer = Comparer.Default; while (e.MoveNext()) { TResult x = selector(e.Current); @@ -1017,10 +1017,7 @@ public static decimal Max(this IEnumerable source, Func e = source.GetEnumerator()) + else { if (!e.MoveNext()) { @@ -1031,7 +1028,7 @@ public static decimal Max(this IEnumerable source, Func 0) + if (Comparer.Default.Compare(x, value) > 0) { value = x; } diff --git a/src/libraries/System.Linq/src/System/Linq/Min.cs b/src/libraries/System.Linq/src/System/Linq/Min.cs index f8c7f3bec469be..f9531778910a42 100644 --- a/src/libraries/System.Linq/src/System/Linq/Min.cs +++ b/src/libraries/System.Linq/src/System/Linq/Min.cs @@ -907,11 +907,10 @@ public static decimal Min(this IEnumerable source, Func comparer = Comparer.Default; TResult? value = default; - if (value == null) + using (IEnumerator e = source.GetEnumerator()) { - using (IEnumerator e = source.GetEnumerator()) + if (value == null) { do { @@ -924,6 +923,7 @@ public static decimal Min(this IEnumerable source, Func comparer = Comparer.Default; while (e.MoveNext()) { TResult x = selector(e.Current); @@ -933,10 +933,7 @@ public static decimal Min(this IEnumerable source, Func e = source.GetEnumerator()) + else { if (!e.MoveNext()) { @@ -947,7 +944,7 @@ public static decimal Min(this IEnumerable source, Func.Default.Compare(x, value) < 0) { value = x; }