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

Expression-bodied lambdas in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
A(x, y) => x * y
B(x, y) => { return x * y; }
C(x, y) => { x * y; }
D(x, y) => return x * y;
What does this expression-bodied lambda do? x => x + 1
AReturns x minus 1
BReturns x plus 1
CPrints x plus 1
DDoes nothing
Can expression-bodied lambdas contain multiple statements?
AYes, always
BOnly if inside braces
CYes, if separated by semicolons
DNo, only one expression
Which keyword is NOT needed in an expression-bodied lambda?
Areturn
B=>
Clambda parameters
Dexpression
How would you write a void expression-bodied lambda that prints a message?
Ax => return Console.WriteLine(x)
Bx => { Console.WriteLine(x); }
Cx => Console.WriteLine(x)
Dx => Console.WriteLine(x); return;
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.