Recall & Review
beginner
What is an expression-bodied lambda in C#?
An expression-bodied lambda is a concise way to write a lambda expression using a single expression without braces and return keyword. It returns the value of that expression directly.
Click to reveal answer
beginner
How do you write a simple expression-bodied lambda that adds two integers a and b?
You write it as: (a, b) => a + b;
Click to reveal answer
intermediate
What is the difference between a statement-bodied lambda and an expression-bodied lambda?
A statement-bodied lambda uses braces and can have multiple statements, requiring a return keyword if it returns a value. An expression-bodied lambda has no braces and returns the result of a single expression directly.
Click to reveal answer
intermediate
Can expression-bodied lambdas be used with void-returning methods?
Yes, expression-bodied lambdas can be used with void-returning methods if the expression is a statement like a method call. For example: x => Console.WriteLine(x);
Click to reveal answer
beginner
Why are expression-bodied lambdas useful?
They make code shorter and easier to read when the lambda only needs to return the result of a simple expression.
Click to reveal answer
Which of the following is a valid expression-bodied lambda in C#?
✗ Incorrect
Option A is an expression-bodied lambda with a single expression. Option B is a statement-bodied lambda. Option C is invalid because it lacks return. Option D is invalid syntax.
What does this expression-bodied lambda do? x => x + 1
✗ Incorrect
The lambda takes x and returns x plus 1.
Can expression-bodied lambdas contain multiple statements?
✗ Incorrect
Expression-bodied lambdas can only have one expression. Multiple statements require braces and become statement-bodied lambdas.
Which keyword is NOT needed in an expression-bodied lambda?
✗ Incorrect
Expression-bodied lambdas return the expression value implicitly, so 'return' is not used.
How would you write a void expression-bodied lambda that prints a message?
✗ Incorrect
Option C is a valid void expression-bodied lambda. Option B is statement-bodied. Options A and D are invalid syntax.
Explain what an expression-bodied lambda is and how it differs from a statement-bodied lambda.
Think about how many statements each can have and how they return values.
You got /5 concepts.
Write an example of an expression-bodied lambda that takes two numbers and returns their product.
Use => with a simple multiplication expression.
You got /4 concepts.