Thursday, June 19, 2008

C# Puzzle No.5 (intermediate)

Can you predict the output of the code above without actually running it?

List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
list.FindAll( i => { Console.WriteLine( i ); return i < 5; } );

2 comments:

apl said...

Documentation states that List(Of T).FindAll "is an O(n) operation, where n is Count", we can guess that the delegate passed as the argument is always called for each item in the list. The output should be:

1
2
3
4
5
6
7
8
9
10

stic said...

For every element from list we will try to evaluate the lambda expression, which will print the value. So Aleksander gave correct answer.

Btw. How many puzzles will are you going to throw at us?