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; }