Can you explain in brief about Aggregate() extension method in LINQ ?

 Posted by Akiii on 3/6/2012 | Category: LINQ Interview questions | Views: 3431 | Points: 40
Answer:


public static int display()
{
int[] numbersArray = { 1, 2, 3, 4, 5 };

return numbersArray.Aggregate((x1, x2) => x1 * x2);
}


output : 120


In the above code, "numbersArray" is an integer array. Here, the first one being the first number or the previous result, and the second one is the second or next number to participate the calculation.

The calculation goes like :-

1 * 1 = 1 (stored in x1)
x1 * 2 = 2 (stored in x1)
x1 * 3 = 6 (stored in x1)
x1 * 4 = 24 (stored in x1)
x1 * 5 = 120 (stored and returned back)



Thanks and Regards
Akiii


Source: MSDN | | Alert Moderator 

Comments or Responses

Login to post response