Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Expression-bodied lambdas in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Expression-bodied lambdas
Define lambda with => expression
↓
Invoke lambda
↓
Evaluate expression
↓
Return result
An expression-bodied lambda uses => to define a function with a single expression that returns a value immediately.
Execution Sample
C Sharp (C#)
Func<int, int> square = x => x * x;
int result = square(3);
Console.WriteLine(result);
Defines a lambda that squares a number and prints the result of squaring 3.
Execution Table
StepActionExpression EvaluatedResult
1Define lambda 'square' as x => x * xx * xLambda stored, no output
2Call square(3)3 * 39
3Print resultConsole.WriteLine(9)Outputs 9
💡 Program ends after printing the result 9
Variable Tracker
VariableStartAfter Step 1After Step 2Final
squarenulllambda function (x => x * x)lambda functionlambda function
resultundefinedundefined99
Key Moments - 2 Insights
Why does the lambda not need curly braces or a return statement?
Because expression-bodied lambdas use a single expression after => which automatically returns its value, as shown in step 1 and 2 of the execution_table.
What happens when we call square(3)?
The expression x * x is evaluated with x=3, resulting in 9, as shown in step 2 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 2?
Aundefined
B9
C3
Dnull
💡 Hint
Check the 'Result' column in row for step 2 in execution_table.
At which step is the lambda function assigned to 'square'?
AStep 3
BStep 2
CStep 1
DBefore Step 1
💡 Hint
Look at the 'Action' column in execution_table for when 'square' is defined.
If we changed the lambda to x => x + 1, what would be the output at step 3?
A4
B9
C3
D1
💡 Hint
Consider the expression evaluated in step 2 and how it affects the printed result in step 3.
Concept Snapshot
Expression-bodied lambdas use => followed by a single expression.
They automatically return the expression's value.
No curly braces or return keyword needed.
Example: Func<int,int> square = x => x * x;
Call like: square(3) returns 9.
Full Transcript
This example shows how expression-bodied lambdas work in C#. First, we define a lambda named 'square' that takes an integer x and returns x multiplied by itself. This is done using the => syntax with a single expression 'x * x'. When we call square(3), the expression evaluates to 9. Finally, we print the result 9 to the console. The lambda does not need curly braces or a return statement because the expression after => is automatically returned.

Practice

(1/5)
1. What is the main purpose of using an expression-bodied lambda in C#?
easy
A. To write a short function with a single expression without braces or return keyword
B. To declare a variable with a complex type
C. To create a class with multiple methods
D. To handle exceptions in a concise way

Solution

  1. Step 1: Understand expression-bodied lambda syntax

    Expression-bodied lambdas use the => operator followed by a single expression, simplifying function syntax.
  2. Step 2: Identify the purpose

    This syntax is used to write short functions without braces or explicit return statements.
  3. Final Answer:

    To write a short function with a single expression without braces or return keyword -> Option A
  4. Quick Check:

    Expression-bodied lambda = short single-expression function [OK]
Hint: Remember: expression-bodied lambdas simplify single-expression functions [OK]
Common Mistakes:
  • Confusing with variable declarations
  • Thinking it creates classes or handles exceptions
  • Assuming it requires braces and return keyword
2. Which of the following is the correct syntax for an expression-bodied lambda that adds two integers a and b?
easy
A. (a, b) => a + b
B. (a, b) => a + b;
C. (a, b) => { return a + b; }
D. (a, b) => { a + b }

Solution

  1. Step 1: Identify expression-bodied lambda syntax

    Expression-bodied lambdas use => followed by a single expression without braces or return keyword.
  2. 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).
  3. Final Answer:

    (a, b) => a + b -> Option A
  4. Quick Check:

    Expression-bodied lambda syntax = (params) => expression [OK]
Hint: No braces or return keyword in expression-bodied lambdas [OK]
Common Mistakes:
  • Adding braces and return keyword
  • Including semicolon after expression
  • Using braces without return keyword
3. What is the output of the following C# code?
Func<int, int> square = x => x * x;
Console.WriteLine(square(4));
medium
A. 8
B. 4
C. 16
D. Compilation error

Solution

  1. Step 1: Understand the lambda function

    The lambda expression x => x * x defines a function that returns the square of x.
  2. Step 2: Evaluate the function call

    Calling square(4) computes 4 * 4 = 16.
  3. Final Answer:

    16 -> Option C
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Multiply input by itself to get square [OK]
Common Mistakes:
  • Confusing output with input
  • Expecting compilation error
  • Misreading the lambda expression
4. Identify the error in the following expression-bodied lambda:
Func<int, int> increment = x => { x + 1; };
medium
A. Semicolon after expression is invalid
B. Braces are not allowed in expression-bodied lambdas
C. Lambda must have two parameters
D. Missing return keyword inside braces

Solution

  1. Step 1: Analyze the lambda syntax

    The lambda uses braces, so it must have an explicit return statement to return a value.
  2. Step 2: Identify the missing return

    Inside braces, the expression x + 1 is evaluated but not returned, causing a compile error.
  3. Final Answer:

    Missing return keyword inside braces -> Option D
  4. Quick Check:

    Braces require return keyword [OK]
Hint: Use return inside braces or remove braces for expression-bodied lambda [OK]
Common Mistakes:
  • Assuming braces are disallowed
  • Thinking semicolon after expression is invalid
  • Expecting multiple parameters
5. Given a list of integers, which expression-bodied lambda correctly filters out even numbers and returns their squares?
var numbers = new List<int> {1, 2, 3, 4, 5};
var result = numbers.Where( /* lambda here */ ).Select(x => x * x).ToList();
hard
A. x => x % 2 == 0
B. x => x % 2 != 0
C. x => { return x % 2 != 0; }
D. x => { x % 2 != 0; }

Solution

  1. Step 1: Understand filtering condition

    We want to keep odd numbers, so the condition is x % 2 != 0.
  2. 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).
  3. Final Answer:

    x => x % 2 != 0 -> Option B
  4. Quick Check:

    Filter odds with expression-bodied lambda [OK]
Hint: No braces and return for expression-bodied lambdas [OK]
Common Mistakes:
  • Using braces without return
  • Choosing wrong condition for filtering
  • Mixing expression-bodied with block-bodied syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard