What if you could shrink your function code to a single, elegant line?
Why Expression-bodied lambdas in C Sharp (C#)? - Purpose & Use Cases
Imagine you need to write a simple function that adds two numbers. You write a full method with curly braces, return statements, and all the extra lines just to do a tiny calculation.
This approach makes your code longer and harder to read. When you have many small functions, the clutter slows you down and makes it easy to miss mistakes.
Expression-bodied lambdas let you write these small functions in one clean line. This keeps your code short, clear, and easy to understand.
Func<int, int, int> add = (x, y) => { return x + y; };Func<int, int, int> add = (x, y) => x + y;
You can write concise, readable functions that focus on what matters without extra noise.
When filtering a list of numbers to find all even ones, expression-bodied lambdas let you write the condition simply and clearly.
Manual methods for small functions add unnecessary clutter.
Expression-bodied lambdas simplify code to one clear line.
This makes your code easier to read and maintain.
Practice
Solution
Step 1: Understand expression-bodied lambda syntax
Expression-bodied lambdas use the => operator followed by a single expression, simplifying function syntax.Step 2: Identify the purpose
This syntax is used to write short functions without braces or explicit return statements.Final Answer:
To write a short function with a single expression without braces or return keyword -> Option AQuick Check:
Expression-bodied lambda = short single-expression function [OK]
- Confusing with variable declarations
- Thinking it creates classes or handles exceptions
- Assuming it requires braces and return keyword
a and b?Solution
Step 1: Identify expression-bodied lambda syntax
Expression-bodied lambdas use => followed by a single expression without braces or return keyword.Step 2: Check each option
(a, b) => a + b correctly uses (a, b) => a + b without braces or return. (a, b) => { return a + b; } uses braces and return (not expression-bodied). (a, b) => a + b; has a semicolon after expression (not needed). (a, b) => { a + b } uses braces without return (invalid).Final Answer:
(a, b) => a + b -> Option AQuick Check:
Expression-bodied lambda syntax = (params) => expression [OK]
- Adding braces and return keyword
- Including semicolon after expression
- Using braces without return keyword
Func<int, int> square = x => x * x; Console.WriteLine(square(4));
Solution
Step 1: Understand the lambda function
The lambda expression x => x * x defines a function that returns the square of x.Step 2: Evaluate the function call
Calling square(4) computes 4 * 4 = 16.Final Answer:
16 -> Option CQuick Check:
4 squared = 16 [OK]
- Confusing output with input
- Expecting compilation error
- Misreading the lambda expression
Func<int, int> increment = x => { x + 1; };Solution
Step 1: Analyze the lambda syntax
The lambda uses braces, so it must have an explicit return statement to return a value.Step 2: Identify the missing return
Inside braces, the expression x + 1 is evaluated but not returned, causing a compile error.Final Answer:
Missing return keyword inside braces -> Option DQuick Check:
Braces require return keyword [OK]
- Assuming braces are disallowed
- Thinking semicolon after expression is invalid
- Expecting multiple parameters
var numbers = new List<int> {1, 2, 3, 4, 5};
var result = numbers.Where( /* lambda here */ ).Select(x => x * x).ToList();Solution
Step 1: Understand filtering condition
We want to keep odd numbers, so the condition is x % 2 != 0.Step 2: Choose correct lambda syntax
x => x % 2 != 0 uses expression-bodied lambda without braces, correctly returning the condition. x => x % 2 == 0 keeps even numbers (wrong). x => { return x % 2 != 0; } uses braces with return (valid but not expression-bodied). x => { x % 2 != 0; } uses braces without return (invalid).Final Answer:
x => x % 2 != 0 -> Option BQuick Check:
Filter odds with expression-bodied lambda [OK]
- Using braces without return
- Choosing wrong condition for filtering
- Mixing expression-bodied with block-bodied syntax
Ready to prove it?
Pass the test and this pattern moves unknown
