0
0
C Sharp (C#)programming~20 mins

Lambda with captures (closures) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Closure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of lambda capturing a variable
What is the output of this C# code that uses a lambda capturing a variable?
C Sharp (C#)
int x = 5;
Func<int> f = () => x * 2;
x = 10;
Console.WriteLine(f());
A20
BCompilation error
C10
D5
Attempts:
2 left
💡 Hint
Remember that lambdas capture variables by reference, not by value.
Predict Output
intermediate
2:00remaining
Output of multiple lambdas capturing loop variable
What is the output of this C# code with lambdas capturing a loop variable?
C Sharp (C#)
var actions = new List<Action>();
for (int i = 0; i < 3; i++)
{
    actions.Add(() => Console.Write(i + " "));
}
foreach (var action in actions) action();
A0 1 2
BCompilation error
C0 0 0
D3 3 3
Attempts:
2 left
💡 Hint
Consider how the loop variable i is captured by the lambdas.
🔧 Debug
advanced
2:00remaining
Why does this lambda capture cause unexpected output?
This code intends to print 0, 1, 2 but prints 3, 3, 3 instead. Why?
C Sharp (C#)
var actions = new List<Action>();
for (int i = 0; i < 3; i++)
{
    actions.Add(() => Console.Write(i + " "));
}
foreach (var action in actions) action();
AThe list actions is cleared before printing, so output is from a default value.
BThe lambda syntax is incorrect and causes all lambdas to share the same output.
CThe variable i is captured by reference, so all lambdas see the final value 3.
DThe loop variable i is copied inside the lambda, but the copy is overwritten.
Attempts:
2 left
💡 Hint
Think about how variables in loops are captured by lambdas in C#.
📝 Syntax
advanced
2:00remaining
Which lambda correctly captures a local variable inside a loop?
Which option correctly captures the loop variable so that each lambda prints 0, 1, and 2 respectively?
C Sharp (C#)
var actions = new List<Action>();
for (int i = 0; i < 3; i++)
{
    // capture i correctly here
}
foreach (var action in actions) action();
Aactions.Add(() => Console.Write(i + " "));
B
int copy = i;
actions.Add(() =&gt; Console.Write(copy + " "));
Cactions.Add(delegate { Console.Write(i + " "); });
Dactions.Add(() => Console.Write((i + 1) + " "));
Attempts:
2 left
💡 Hint
Try creating a new variable inside the loop to hold the current value.
🚀 Application
expert
3:00remaining
How many unique values are captured by these lambdas?
Consider this code snippet. How many unique values are printed when invoking all lambdas in the list?
C Sharp (C#)
var funcs = new List<Func<int>>();
int x = 1;
for (int i = 0; i < 3; i++)
{
    int y = i;
    funcs.Add(() => x + y);
}
x = 5;
var results = funcs.Select(f => f()).ToList();
Console.WriteLine(string.Join(",", results));
A3 unique values: 5,6,7
B3 unique values: 2,3,4
C1 unique value: 8
D1 unique value: 6
Attempts:
2 left
💡 Hint
Remember which variables are captured by reference and which by value.