Recall & Review
beginner
What is a lambda expression in C#?
A lambda expression is a short way to write anonymous functions using the syntax (input) => expression or (input) => { statements }. It is often used for inline code like in LINQ queries or event handlers.
Click to reveal answer
beginner
How do you write a lambda expression that takes one integer and returns its square?
You write it as: x => x * x. Here, x is the input parameter, and x * x is the expression returned.
Click to reveal answer
intermediate
What is the difference between a lambda expression with an expression body and one with a statement body?
An expression body lambda returns the result of a single expression directly, like x => x + 1. A statement body lambda uses braces and can have multiple statements, like x => { int y = x + 1; return y; }.
Click to reveal answer
beginner
Can a lambda expression have multiple parameters? Show an example.
Yes, a lambda can have multiple parameters separated by commas. Example: (x, y) => x + y adds two numbers.
Click to reveal answer
intermediate
How do you specify the type of parameters in a lambda expression?
You can specify types explicitly like (int x, int y) => x + y, but usually the compiler infers the types from context.
Click to reveal answer
Which of the following is a valid lambda expression in C#?
✗ Incorrect
Option A is correct. Option B is valid syntax but not the best choice here since the question implies invalidity; Option C is missing a semicolon after return, which is corrected here. Option D is missing the parameter.
What does the lambda expression (x, y) => x - y do?
✗ Incorrect
The expression subtracts y from x.
How do you write a lambda expression with no parameters that returns 42?
✗ Incorrect
Option C correctly shows a lambda with no parameters returning 42.
Which syntax is used for a statement body lambda?
✗ Incorrect
Option A uses braces and a return statement, which is the statement body syntax.
Can the compiler infer parameter types in lambda expressions?
✗ Incorrect
The compiler infers types when it can, based on the context where the lambda is used.
Explain the basic syntax of a lambda expression in C# and give an example.
Think about how you write a small function without a name.
You got /4 concepts.
Describe the difference between expression body and statement body lambdas with examples.
One is short and simple, the other can do more steps.
You got /3 concepts.