Expression-bodied lambdas let you write short functions in a simple, clean way using just one expression.
Expression-bodied lambdas in C Sharp (C#)
(parameters) => expression
The expression after the arrow is the value returned automatically.
No need for curly braces or explicit return keyword for single expressions.
x => x * x
(a, b) => a + b
() => DateTime.Now
This program shows three expression-bodied lambdas: one to square a number, one to add two numbers, and one to return a greeting message.
using System; class Program { static void Main() { Func<int, int> square = x => x * x; Func<int, int, int> add = (a, b) => a + b; Func<string> greet = () => "Hello!"; Console.WriteLine(square(5)); Console.WriteLine(add(3, 4)); Console.WriteLine(greet()); } }
Expression-bodied lambdas are great for simple logic but use regular lambdas with braces for multiple statements.
They help keep your code short and easy to read.
Expression-bodied lambdas use a single expression after the arrow to return a value.
They simplify writing short functions without braces or return statements.
Useful for quick, readable code especially when passing functions as arguments.
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
