@@ -36,20 +36,24 @@ public static Range<int> MinMax(this IEnumerable<int> source)
3636 if ( source == null )
3737 throw new ArgumentNullException ( "source" ) ;
3838
39- if ( ! source . Any ( ) )
40- throw new InvalidOperationException ( "source sequence contains no elements" ) ;
41-
4239 // initialize minimum to max possible value and maximum to minimum possible value
4340 // so that the first comparisons in the aggregate function work as expected
4441 var minMax = new Range < int > ( int . MaxValue , int . MinValue , true ) ;
4542
46- return source . Aggregate < int , Range < int > > ( minMax , ( accumulator , value ) =>
43+ long n = 0 ;
44+ var result = source . Aggregate < int , Range < int > > ( minMax , ( accumulator , value ) =>
4745 {
4846 var min = Math . Min ( accumulator . Min , value ) ;
4947 var max = Math . Max ( accumulator . Max , value ) ;
50-
48+ n ++ ;
49+
5150 return new Range < int > ( min , max ) ;
5251 } ) ;
52+
53+ if ( n > 0 )
54+ return result ;
55+
56+ throw new InvalidOperationException ( "source sequence contains no elements" ) ;
5357 }
5458
5559 /// <summary>
@@ -114,20 +118,24 @@ public static Range<long> MinMax(this IEnumerable<long> source)
114118 if ( source == null )
115119 throw new ArgumentNullException ( "source" ) ;
116120
117- if ( ! source . Any ( ) )
118- throw new InvalidOperationException ( "source sequence contains no elements" ) ;
119-
120121 // initialize minimum to max possible value and maximum to minimum possible value
121122 // so that the first comparisons in the aggregate function work as expected
122123 var minMax = new Range < long > ( long . MaxValue , long . MinValue , true ) ;
123124
124- return source . Aggregate < long , Range < long > > ( minMax , ( accumulator , value ) =>
125+ long n = 0 ;
126+ var result = source . Aggregate < long , Range < long > > ( minMax , ( accumulator , value ) =>
125127 {
126128 var min = Math . Min ( accumulator . Min , value ) ;
127129 var max = Math . Max ( accumulator . Max , value ) ;
128-
130+ n ++ ;
131+
129132 return new Range < long > ( min , max ) ;
130133 } ) ;
134+
135+ if ( n > 0 )
136+ return result ;
137+
138+ throw new InvalidOperationException ( "source sequence contains no elements" ) ;
131139 }
132140
133141 /// <summary>
@@ -192,20 +200,24 @@ public static Range<float> MinMax(this IEnumerable<float> source)
192200 if ( source == null )
193201 throw new ArgumentNullException ( "source" ) ;
194202
195- if ( ! source . Any ( ) )
196- throw new InvalidOperationException ( "source sequence contains no elements" ) ;
197-
198203 // initialize minimum to max possible value and maximum to minimum possible value
199204 // so that the first comparisons in the aggregate function work as expected
200205 var minMax = new Range < float > ( float . MaxValue , float . MinValue , true ) ;
201206
202- return source . Aggregate < float , Range < float > > ( minMax , ( accumulator , value ) =>
207+ long n = 0 ;
208+ var result = source . Aggregate < float , Range < float > > ( minMax , ( accumulator , value ) =>
203209 {
204210 var min = Math . Min ( accumulator . Min , value ) ;
205211 var max = Math . Max ( accumulator . Max , value ) ;
206-
212+ n ++ ;
213+
207214 return new Range < float > ( min , max ) ;
208215 } ) ;
216+
217+ if ( n > 0 )
218+ return result ;
219+
220+ throw new InvalidOperationException ( "source sequence contains no elements" ) ;
209221 }
210222
211223 /// <summary>
@@ -270,20 +282,24 @@ public static Range<double> MinMax(this IEnumerable<double> source)
270282 if ( source == null )
271283 throw new ArgumentNullException ( "source" ) ;
272284
273- if ( ! source . Any ( ) )
274- throw new InvalidOperationException ( "source sequence contains no elements" ) ;
275-
276285 // initialize minimum to max possible value and maximum to minimum possible value
277286 // so that the first comparisons in the aggregate function work as expected
278287 var minMax = new Range < double > ( double . MaxValue , double . MinValue , true ) ;
279288
280- return source . Aggregate < double , Range < double > > ( minMax , ( accumulator , value ) =>
289+ long n = 0 ;
290+ var result = source . Aggregate < double , Range < double > > ( minMax , ( accumulator , value ) =>
281291 {
282292 var min = Math . Min ( accumulator . Min , value ) ;
283293 var max = Math . Max ( accumulator . Max , value ) ;
284-
294+ n ++ ;
295+
285296 return new Range < double > ( min , max ) ;
286297 } ) ;
298+
299+ if ( n > 0 )
300+ return result ;
301+
302+ throw new InvalidOperationException ( "source sequence contains no elements" ) ;
287303 }
288304
289305 /// <summary>
@@ -348,20 +364,24 @@ public static Range<decimal> MinMax(this IEnumerable<decimal> source)
348364 if ( source == null )
349365 throw new ArgumentNullException ( "source" ) ;
350366
351- if ( ! source . Any ( ) )
352- throw new InvalidOperationException ( "source sequence contains no elements" ) ;
353-
354367 // initialize minimum to max possible value and maximum to minimum possible value
355368 // so that the first comparisons in the aggregate function work as expected
356369 var minMax = new Range < decimal > ( decimal . MaxValue , decimal . MinValue , true ) ;
357370
358- return source . Aggregate < decimal , Range < decimal > > ( minMax , ( accumulator , value ) =>
371+ long n = 0 ;
372+ var result = source . Aggregate < decimal , Range < decimal > > ( minMax , ( accumulator , value ) =>
359373 {
360374 var min = Math . Min ( accumulator . Min , value ) ;
361375 var max = Math . Max ( accumulator . Max , value ) ;
362-
376+ n ++ ;
377+
363378 return new Range < decimal > ( min , max ) ;
364379 } ) ;
380+
381+ if ( n > 0 )
382+ return result ;
383+
384+ throw new InvalidOperationException ( "source sequence contains no elements" ) ;
365385 }
366386
367387 /// <summary>
0 commit comments