Permalink
Cannot retrieve contributors at this time
87 lines (72 sloc)
2.65 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
runtime/src/libraries/System.Linq/src/System/Linq/Aggregate.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Licensed to the .NET Foundation under one or more agreements. | |
// The .NET Foundation licenses this file to you under the MIT license. | |
// See the LICENSE file in the project root for more information. | |
using System.Collections.Generic; | |
namespace System.Linq | |
{ | |
public static partial class Enumerable | |
{ | |
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func) | |
{ | |
if (source == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | |
} | |
if (func == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); | |
} | |
using (IEnumerator<TSource> e = source.GetEnumerator()) | |
{ | |
if (!e.MoveNext()) | |
{ | |
ThrowHelper.ThrowNoElementsException(); | |
} | |
TSource result = e.Current; | |
while (e.MoveNext()) | |
{ | |
result = func(result, e.Current); | |
} | |
return result; | |
} | |
} | |
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func) | |
{ | |
if (source == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | |
} | |
if (func == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); | |
} | |
TAccumulate result = seed; | |
foreach (TSource element in source) | |
{ | |
result = func(result, element); | |
} | |
return result; | |
} | |
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector) | |
{ | |
if (source == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); | |
} | |
if (func == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.func); | |
} | |
if (resultSelector == null) | |
{ | |
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.resultSelector); | |
} | |
TAccumulate result = seed; | |
foreach (TSource element in source) | |
{ | |
result = func(result, element); | |
} | |
return resultSelector(result); | |
} | |
} | |
} |