Applies an accumulator function over a sequence.
Assembly: System.Core (in System.Core.dll)
<[%$TOPIC/bb548651_en-us_VS_110_1_0_0_0_0%]> _ Public Shared Function Aggregate(Of TSource) ( _ source As [%$TOPIC/bb548651_en-us_VS_110_1_0_0_0_1%](Of TSource), _ func As [%$TOPIC/bb548651_en-us_VS_110_1_0_0_0_2%](Of TSource, TSource, TSource) _ ) As TSource
public static TSource Aggregate<TSource>( this [%$TOPIC/bb548651_en-us_VS_110_1_0_1_0_0%]<TSource> source, [%$TOPIC/bb548651_en-us_VS_110_1_0_1_0_1%]<TSource, TSource, TSource> func )
[[%$TOPIC/bb548651_en-us_VS_110_1_0_2_0_0%]] public: generic<typename TSource> static TSource Aggregate( [%$TOPIC/bb548651_en-us_VS_110_1_0_2_0_1%]<TSource>^ source, [%$TOPIC/bb548651_en-us_VS_110_1_0_2_0_2%]<TSource, TSource, TSource>^ func )
static member Aggregate : source:[%$TOPIC/bb548651_en-us_VS_110_1_0_3_0_0%]<'TSource> * func:[%$TOPIC/bb548651_en-us_VS_110_1_0_3_0_1%]<'TSource, 'TSource, 'TSource> -> 'TSource
The type of the elements of source.
An accumulator function to be invoked on each element.
The final accumulator value.
Usage NoteIn Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
The method makes it simple to perform a calculation over a sequence of values. This method works by calling func one time for each element in source except the first one. Each time func is called, passes both the element from the sequence and an aggregated value (as the first argument to func). The first element of source is used as the initial aggregate value. The result of func replaces the previous aggregated value. returns the final result of func.
This overload of the Aggregate method isn't suitable for all cases because it uses the first element of source as the initial aggregate value. You should choose another overload if the return value should include only the elements of source that meet a certain condition. For example, this overload isn't reliable if you want to calculate the sum of the even numbers in source. The result will be incorrect if the first element is odd instead of even.
The following code example demonstrates how to reverse the order of words in a string by using Aggregate.
Sub AggregateEx1() Dim sentence As String = _ "the quick brown fox jumps over the lazy dog" ' Split the string into individual words. Dim words() As String = sentence.Split(" "c) ' Prepend each word to the beginning of the new sentence to reverse the word order. Dim reversed As String = _ words.Aggregate(Function(ByVal current, ByVal word) word & " " & current) ' Display the output. MsgBox(reversed) End Sub ' This code produces the following output: ' ' dog lazy the over jumps fox brown quick the
string sentence = "the quick brown fox jumps over the lazy dog"; // Split the string into individual words. string words = sentence.Split(' '); // Prepend each word to the beginning of the // new sentence to reverse the word order. string reversed = words.Aggregate((workingSentence, next) => next + " " + workingSentence); Console.WriteLine(reversed); // This code produces the following output: // // dog lazy the over jumps fox brown quick the
.NET FrameworkSupported in: 4.5, 4, 3.5
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.