Skip to content

Commit

Permalink
Code modernization in System.Linq (#110910)
Browse files Browse the repository at this point in the history
* Use collection initializers in System.Linq

* Use using declarations in System.Linq
  • Loading branch information
roji authored Jan 2, 2025
1 parent 95814d0 commit 3d39be4
Show file tree
Hide file tree
Showing 88 changed files with 3,617 additions and 3,684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public override List<TSource> ToList()
return new List<TSource>(1) { _item };
}

List<TSource> list = count == -1 ? new List<TSource>() : new List<TSource>(count);
List<TSource> list = count == -1 ? [] : new List<TSource>(count);
if (!_appending)
{
list.Add(_item);
Expand Down Expand Up @@ -257,7 +257,7 @@ public override TSource[] ToArray()
public override List<TSource> ToList()
{
int count = GetCount(onlyIfCheap: true);
List<TSource> list = count == -1 ? new List<TSource>() : new List<TSource>(count);
List<TSource> list = count == -1 ? [] : new List<TSource>(count);

_prepended?.Fill(SetCountAndGetSpan(list, _prependCount));

Expand Down
118 changes: 56 additions & 62 deletions src/libraries/System.Linq/src/System/Linq/Average.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public static double Average(this IEnumerable<int> source)

return (double)sum / span.Length;
}

using (IEnumerator<int> e = source.GetEnumerator())
else
{
using IEnumerator<int> e = source.GetEnumerator();

if (!e.MoveNext())
{
ThrowHelper.ThrowNoElementsException();
Expand All @@ -64,6 +65,7 @@ public static double Average(this IEnumerable<int> source)
while (e.MoveNext())
{
checked { sum += e.Current; }

count++;
}

Expand Down Expand Up @@ -99,23 +101,21 @@ private static TResult Average<TSource, TAccumulator, TResult>(this IEnumerable<
return TResult.CreateChecked(Sum<TSource, TAccumulator>(span)) / TResult.CreateChecked(span.Length);
}

using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
if (!e.MoveNext())
{
if (!e.MoveNext())
{
ThrowHelper.ThrowNoElementsException();
}

TAccumulator sum = TAccumulator.CreateChecked(e.Current);
long count = 1;
while (e.MoveNext())
{
checked { sum += TAccumulator.CreateChecked(e.Current); }
count++;
}
ThrowHelper.ThrowNoElementsException();
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
TAccumulator sum = TAccumulator.CreateChecked(e.Current);
long count = 1;
while (e.MoveNext())
{
checked { sum += TAccumulator.CreateChecked(e.Current); }
count++;
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}


Expand All @@ -139,28 +139,26 @@ private static TResult Average<TSource, TAccumulator, TResult>(this IEnumerable<
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

using (IEnumerator<TSource?> e = source.GetEnumerator())
using IEnumerator<TSource?> e = source.GetEnumerator();
while (e.MoveNext())
{
while (e.MoveNext())
TSource? value = e.Current;
if (value.HasValue)
{
TSource? value = e.Current;
if (value.HasValue)
{
TAccumulator sum = TAccumulator.CreateChecked(value.GetValueOrDefault());
long count = 1;
TAccumulator sum = TAccumulator.CreateChecked(value.GetValueOrDefault());
long count = 1;

while (e.MoveNext())
while (e.MoveNext())
{
value = e.Current;
if (value.HasValue)
{
value = e.Current;
if (value.HasValue)
{
checked { sum += TAccumulator.CreateChecked(value.GetValueOrDefault()); }
count++;
}
checked { sum += TAccumulator.CreateChecked(value.GetValueOrDefault()); }
count++;
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}
}

Expand Down Expand Up @@ -193,24 +191,22 @@ private static TResult Average<TSource, TSelector, TAccumulator, TResult>(this I
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}

using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
if (!e.MoveNext())
{
if (!e.MoveNext())
{
ThrowHelper.ThrowNoElementsException();
}
ThrowHelper.ThrowNoElementsException();
}

TAccumulator sum = TAccumulator.CreateChecked(selector(e.Current));
long count = 1;
TAccumulator sum = TAccumulator.CreateChecked(selector(e.Current));
long count = 1;

while (e.MoveNext())
{
checked { sum += TAccumulator.CreateChecked(selector(e.Current)); }
count++;
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
while (e.MoveNext())
{
checked { sum += TAccumulator.CreateChecked(selector(e.Current)); }
count++;
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}


Expand Down Expand Up @@ -239,28 +235,26 @@ private static TResult Average<TSource, TSelector, TAccumulator, TResult>(this I
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}

using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
while (e.MoveNext())
{
while (e.MoveNext())
TSelector? value = selector(e.Current);
if (value.HasValue)
{
TSelector? value = selector(e.Current);
if (value.HasValue)
{
TAccumulator sum = TAccumulator.CreateChecked(value.GetValueOrDefault());
long count = 1;
TAccumulator sum = TAccumulator.CreateChecked(value.GetValueOrDefault());
long count = 1;

while (e.MoveNext())
while (e.MoveNext())
{
value = selector(e.Current);
if (value.HasValue)
{
value = selector(e.Current);
if (value.HasValue)
{
checked { sum += TAccumulator.CreateChecked(value.GetValueOrDefault()); }
count++;
}
checked { sum += TAccumulator.CreateChecked(value.GetValueOrDefault()); }
count++;
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}

return TResult.CreateChecked(sum) / TResult.CreateChecked(count);
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/libraries/System.Linq/src/System/Linq/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ public static int Count<TSource>(this IEnumerable<TSource> source)
}

int count = 0;
using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
checked
{
checked
while (e.MoveNext())
{
while (e.MoveNext())
{
count++;
}
count++;
}
}

Expand Down Expand Up @@ -150,12 +148,10 @@ public static long LongCount<TSource>(this IEnumerable<TSource> source)
// the source can't possibly be something from which we can extract a span.

long count = 0;
using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
while (e.MoveNext())
{
while (e.MoveNext())
{
checked { count++; }
}
checked { count++; }
}

return count;
Expand Down
10 changes: 4 additions & 6 deletions src/libraries/System.Linq/src/System/Linq/First.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,11 @@ public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source,
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
if (e.MoveNext())
{
if (e.MoveNext())
{
found = true;
return e.Current;
}
found = true;
return e.Current;
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/libraries/System.Linq/src/System/Linq/GroupJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this

private static IEnumerable<TResult> GroupJoinIterator<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey>? comparer)
{
using (IEnumerator<TOuter> e = outer.GetEnumerator())
using IEnumerator<TOuter> e = outer.GetEnumerator();
if (e.MoveNext())
{
if (e.MoveNext())
Lookup<TKey, TInner> lookup = Lookup<TKey, TInner>.CreateForJoin(inner, innerKeySelector, comparer);
do
{
Lookup<TKey, TInner> lookup = Lookup<TKey, TInner>.CreateForJoin(inner, innerKeySelector, comparer);
do
{
TOuter item = e.Current;
yield return resultSelector(item, lookup[outerKeySelector(item)]);
}
while (e.MoveNext());
TOuter item = e.Current;
yield return resultSelector(item, lookup[outerKeySelector(item)]);
}
while (e.MoveNext());
}
}
}
Expand Down
30 changes: 14 additions & 16 deletions src/libraries/System.Linq/src/System/Linq/Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,27 @@ public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnu

private static IEnumerable<TResult> JoinIterator<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey>? comparer)
{
using (IEnumerator<TOuter> e = outer.GetEnumerator())
using IEnumerator<TOuter> e = outer.GetEnumerator();

if (e.MoveNext())
{
if (e.MoveNext())
Lookup<TKey, TInner> lookup = Lookup<TKey, TInner>.CreateForJoin(inner, innerKeySelector, comparer);
if (lookup.Count != 0)
{
Lookup<TKey, TInner> lookup = Lookup<TKey, TInner>.CreateForJoin(inner, innerKeySelector, comparer);
if (lookup.Count != 0)
do
{
do
TOuter item = e.Current;
Grouping<TKey, TInner>? g = lookup.GetGrouping(outerKeySelector(item), create: false);
if (g is not null)
{
TOuter item = e.Current;
Grouping<TKey, TInner>? g = lookup.GetGrouping(outerKeySelector(item), create: false);
if (g is not null)
int count = g._count;
TInner[] elements = g._elements;
for (int i = 0; i != count; ++i)
{
int count = g._count;
TInner[] elements = g._elements;
for (int i = 0; i != count; ++i)
{
yield return resultSelector(item, elements[i]);
}
yield return resultSelector(item, elements[i]);
}
}
while (e.MoveNext());
}
} while (e.MoveNext());
}
}
}
Expand Down
44 changes: 20 additions & 24 deletions src/libraries/System.Linq/src/System/Linq/Last.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,18 @@ public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, F
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
if (e.MoveNext())
{
if (e.MoveNext())
TSource result;
do
{
TSource result;
do
{
result = e.Current;
}
while (e.MoveNext());

found = true;
return result;
result = e.Current;
}
while (e.MoveNext());

found = true;
return result;
}
}

Expand Down Expand Up @@ -140,25 +138,23 @@ public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, F
}
else
{
using (IEnumerator<TSource> e = source.GetEnumerator())
using IEnumerator<TSource> e = source.GetEnumerator();
while (e.MoveNext())
{
while (e.MoveNext())
TSource result = e.Current;
if (predicate(result))
{
TSource result = e.Current;
if (predicate(result))
while (e.MoveNext())
{
while (e.MoveNext())
TSource element = e.Current;
if (predicate(element))
{
TSource element = e.Current;
if (predicate(element))
{
result = element;
}
result = element;
}

found = true;
return result;
}

found = true;
return result;
}
}
}
Expand Down
Loading

0 comments on commit 3d39be4

Please sign in to comment.