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

Lambda with captures (closures) in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lambda that adds a captured variable to its input.

C Sharp (C#)
int addValue = 5;
Func<int, int> add = x => x [1] addValue;
int result = add(3);
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to use the captured variable.
2fill in blank
medium

Complete the code to capture a variable and use it inside the lambda to multiply the input.

C Sharp (C#)
int multiplier = 4;
Func<int, int> multiply = n => n [1] multiplier;
int output = multiply(2);
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Not using the captured variable inside the lambda.
3fill in blank
hard

Fix the error in the lambda expression to correctly capture the variable and add it to the input.

C Sharp (C#)
int offset = 10;
Func<int, int> addOffset = (int x) => x [1] offset;
int result = addOffset(5);
Drag options to blanks, or click blank then click option'
A-
B/
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Syntax errors in the lambda expression.
4fill in blank
hard

Fill the blank to create a lambda that captures a variable and returns true if input is greater than that variable.

C Sharp (C#)
int threshold = 7;
Func<int, bool> isGreater = x => x [1] threshold;
bool check = isGreater(10);
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or equality operators instead of greater than.
Not using the captured variable in the comparison.
5fill in blank
hard

Fill all three blanks to create a lambda that captures a string and returns it repeated n times.

C Sharp (C#)
string word = "Hi";
Func<int, string> repeat = n => string.Concat(Enumerable.Repeat([1], [2]));
string result = repeat([3]);
Drag options to blanks, or click blank then click option'
Aword
Bn
C3
D"Hi"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the captured variable.
Mixing up the order of arguments.
Not passing a number for the repeat count.