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

Lambda expression syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Lambda expression syntax
Start
↓
Define parameters
→Use => operator
↓
Lambda created
↓
Use lambda
↓
End
This flow shows how a lambda expression is created by defining parameters, using the => operator, and writing an expression or block, resulting in a lambda that can be used.
Execution Sample
C Sharp (C#)
Func<int, int> square = x => x * x;
int result = square(4);
Console.WriteLine(result);
Defines a lambda that squares a number, calls it with 4, and prints the result.
Execution Table
StepActionEvaluationResult
1Define lambda: x => x * xCreate function that returns x squaredLambda assigned to 'square'
2Call square(4)Calculate 4 * 416
3Print resultOutput 16Console shows: 16
4EndNo more codeExecution stops
💡 Execution stops after printing the result.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
squarenulllambda function (x => x * x)lambda functionlambda function
resultundefinedundefined1616
Key Moments - 3 Insights
Why do we use the => symbol in lambda expressions?
The => symbol separates the input parameters from the expression or block that defines the lambda's behavior, as shown in step 1 of the execution_table.
What happens when we call the lambda with an argument?
The lambda runs the expression using the argument value, like in step 2 where 4 is squared to get 16.
Is the lambda executed when it is defined or when it is called?
The lambda is created when defined (step 1) but only runs when called (step 2), as the execution_table shows.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 2?
Anull
B4
C16
Dundefined
💡 Hint
Check the 'result' variable in variable_tracker after step 2.
At which step is the lambda function assigned to the variable 'square'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the action in execution_table step 1.
If we change the lambda to x => x + 1, what would be the output at step 3?
A16
B5
C4
D1
💡 Hint
The lambda adds 1 to the input; input is 4, so output is 5.
Concept Snapshot
Lambda expression syntax in C#:
(parameters) => expression_or_block
Creates an anonymous function.
Used to write short functions inline.
Called like normal functions.
=> separates parameters and body.
Full Transcript
This visual execution shows how a lambda expression is created and used in C#. First, a lambda is defined with parameters and an expression separated by the => operator. Then, the lambda is called with an argument, which runs the expression and returns a result. Finally, the result is printed. Variables 'square' and 'result' change as the code runs. Key points include understanding the => symbol, when the lambda runs, and how the result is produced.

Practice

(1/5)
1. What does the lambda expression x => x * 2 represent in C#?
easy
A. A variable declaration
B. A function that doubles the input value
C. A loop that runs twice
D. A conditional statement

Solution

  1. Step 1: Understand lambda syntax

    The expression x => x * 2 means a function taking input x and returning x * 2.
  2. Step 2: Interpret the operation

    It doubles the input value by multiplying it by 2.
  3. Final Answer:

    A function that doubles the input value -> Option B
  4. Quick Check:

    Lambda x => x * 2 doubles input [OK]
Hint: Lambda uses => to define short functions quickly [OK]
Common Mistakes:
  • Thinking lambda is a variable
  • Confusing lambda with loops
  • Assuming lambda is a conditional
2. Which of the following is the correct syntax for a lambda expression that takes two integers and returns their sum?
easy
A. (a, b) => a + b
B. a, b => a + b
C. (a, b) => { a + b }
D. (a, b) => a + b;

Solution

  1. Step 1: Check parameter syntax

    Parameters must be enclosed in parentheses when there is more than one, so (a, b) is correct.
  2. Step 2: Check expression body

    The expression a + b without braces is valid for single expressions. (a, b) => a + b matches this.
  3. Final Answer:

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

    Two parameters need parentheses and expression body [OK]
Hint: Use parentheses for multiple parameters in lambda [OK]
Common Mistakes:
  • Omitting parentheses for multiple parameters
  • Adding semicolon inside lambda expression
  • Using braces without return keyword incorrectly
3. What is the output of the following C# code?
Func<int, int> square = x => x * x;
Console.WriteLine(square(4));
medium
A. Compilation error
B. 8
C. 4
D. 16

Solution

  1. Step 1: Understand the lambda function

    The lambda x => x * x returns the square of the input number.
  2. Step 2: Calculate the output for input 4

    4 * 4 = 16, so square(4) returns 16.
  3. Final Answer:

    16 -> Option D
  4. Quick Check:

    Square of 4 is 16 [OK]
Hint: Lambda returns expression result directly if no braces [OK]
Common Mistakes:
  • Confusing multiplication with addition
  • Expecting lambda to print instead of return
  • Thinking lambda causes syntax error
4. Identify the error in this lambda expression:
Func<int, int> add = (x, y) => x + y;
medium
A. Parentheses around parameters are not allowed
B. Missing return keyword inside lambda
C. Lambda has too many parameters for Func<int, int>
D. Lambda expression syntax is correct

Solution

  1. Step 1: Check Func delegate signature

    Func<int, int> expects one input parameter and one output (int).
  2. Step 2: Compare lambda parameters

    The lambda has two parameters (x, y), which does not match the delegate expecting one.
  3. Final Answer:

    Lambda has too many parameters for Func<int, int> -> Option C
  4. Quick Check:

    Func<int, int> means one input, one output [OK]
Hint: Match lambda parameters count with delegate type [OK]
Common Mistakes:
  • Assuming multiple parameters allowed for Func
  • Forgetting delegate parameter count rules
  • Thinking return keyword is always needed
5. Given a list of integers, which lambda expression correctly filters out all even numbers using List<int>.Where()?
var numbers = new List<int> {1, 2, 3, 4, 5, 6};
var odds = numbers.Where( /* which lambda? */ );
hard
A. x => x % 2 == 1
B. x => x % 2 = 1
C. x => { return x % 2 == 0; }
D. x => x / 2 == 1

Solution

  1. Step 1: Understand filtering condition

    We want to keep odd numbers, so condition is x % 2 == 1.
  2. Step 2: Check lambda syntax and logic

    x => x % 2 == 1 uses correct syntax and logic to filter odds. x => x % 2 = 1 uses assignment (=) which is invalid. x => { return x % 2 == 0; } filters evens, not odds. x => x / 2 == 1 checks division, not oddness.
  3. Final Answer:

    x => x % 2 == 1 -> Option A
  4. Quick Check:

    Filter odds with modulo == 1 [OK]
Hint: Use == for comparison, % 2 == 1 filters odd numbers [OK]
Common Mistakes:
  • Using = instead of == in lambda
  • Filtering evens instead of odds
  • Confusing division with modulo
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