c# - Need more details on Enumerable.Aggregate function -
can me understand,
words.aggregate((workingsentence, next) => + next + " " + workingsentence);
from below code snippet? , great if explain me achive in c# 1.1.
(snippet ms)-
string sentence = "the quick brown fox jumps on lazy dog"; // split string individual words. string[] words = sentence.split(' '); // prepend each word beginning of // new sentence reverse word order. string reversed = words.aggregate((workingsentence, next) => next + " " + workingsentence); console.writeline(reversed); // code produces following output: // // dog lazy on jumps fox brown quick
the aggregate
part of example translates this:
string workingsentence = null; bool firstelement = true; foreach (string next in words) { if (firstelement) { workingsentence = next; firstelement = false; } else { workingsentence = next + " " + workingsentence; } } string reversed = workingsentence;
the workingsentence
variable accumulator updated on each iteration of loop applying function existing accumulator value , current element of sequence; performed lambda in example , body of foreach
loop in example.
Comments
Post a Comment