//Applies an accumulator function over a sequence. //public static TSource Aggregate<TSource> //(this System.Collections.Generic.IEnumerable<TSource> source, //Func<TSource,TSource,TSource> func); 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 //Applies an accumulator function over a sequence. The specified seed value is used as the //initial accumulator value. //public static TAccumulate Aggregate<TSource,TAccumulate> (this //System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, //Func<TAccumulate,TSource,TAccumulate> func); int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 }; // Count the even numbers in the array, using a seed value of 0. int numEven = ints.Aggregate(0, (total, next) => next % 2 == 0 ? total + 1 : total); Console.WriteLine("The number of even integers is: {0}", numEven); // This code produces the following output: // // The number of even integers is: 6 //Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value. //public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector); string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" }; // Determine whether any string in the array is longer than "banana". string longestName = fruits.Aggregate("banana", (longest, next) => next.Length > longest.Length ? next : longest, // Return the final result as an upper case string. fruit => fruit.ToUpper()); Console.WriteLine( "The fruit with the longest name is {0}.", longestName); // This code produces the following output: // // The fruit with the longest name is PASSIONFRUIT.
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter