📖 Scenario: You are working on a simple program that processes a list of numbers. You want to use a lambda expression to filter and transform these numbers easily.
🎯 Goal: Build a C# program that uses a lambda expression to select even numbers from a list and then doubles each selected number.
📋 What You'll Learn
Create a list of integers with specific values
Create a lambda expression to filter even numbers
Use the lambda expression to transform the filtered numbers by doubling them
Print the resulting list of doubled even numbers
💡 Why This Matters
🌍 Real World
Filtering and transforming lists of data is common in many programs, such as processing user inputs, handling collections of items, or working with data streams.
💼 Career
Understanding lambda expressions and list operations is essential for C# developers to write concise and readable code, especially when working with collections and LINQ.
Progress0 / 4 steps
1
Create the list of numbers
Create a list of integers called numbers with these exact values: 1, 2, 3, 4, 5, 6.
C Sharp (C#)
Hint
Use List<int> and initialize it with the values inside curly braces.
2
Create a lambda expression to filter even numbers
Create a variable called evenNumbers that uses numbers.FindAll(n => n % 2 == 0) to select only even numbers from numbers.
C Sharp (C#)
Hint
Use FindAll method with a lambda expression n => n % 2 == 0 to get even numbers.
3
Double the filtered even numbers using a lambda expression
Create a variable called doubledEvenNumbers that uses evenNumbers.ConvertAll(n => n * 2) to double each number in evenNumbers.
C Sharp (C#)
Hint
Use ConvertAll with a lambda expression n => n * 2 to double each number.
4
Print the doubled even numbers
Use foreach to print each number in doubledEvenNumbers on its own line.
C Sharp (C#)
Hint
Use a foreach loop to print each number with Console.WriteLine.
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
Step 1: Understand lambda syntax
The expression x => x * 2 means a function taking input x and returning x * 2.
Step 2: Interpret the operation
It doubles the input value by multiplying it by 2.
Final Answer:
A function that doubles the input value -> Option B
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
Step 1: Check parameter syntax
Parameters must be enclosed in parentheses when there is more than one, so (a, b) is correct.
Step 2: Check expression body
The expression a + b without braces is valid for single expressions. (a, b) => a + b matches this.
Final Answer:
(a, b) => a + b -> Option A
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
Step 1: Understand the lambda function
The lambda x => x * x returns the square of the input number.
Step 2: Calculate the output for input 4
4 * 4 = 16, so square(4) returns 16.
Final Answer:
16 -> Option D
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
Step 1: Check Func delegate signature
Func<int, int> expects one input parameter and one output (int).
Step 2: Compare lambda parameters
The lambda has two parameters (x, y), which does not match the delegate expecting one.
Final Answer:
Lambda has too many parameters for Func<int, int> -> Option C
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
Step 1: Understand filtering condition
We want to keep odd numbers, so condition is x % 2 == 1.
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.
Final Answer:
x => x % 2 == 1 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.