What if you could write tiny functions in just one line, right where you need them?
Why Lambda expression syntax in C Sharp (C#)? - Purpose & Use Cases
Imagine you want to write a small piece of code that sorts a list of names by their length. Doing this manually means writing a full method with a name, parameters, and return type just for this tiny task.
Writing full methods for simple tasks is slow and clutters your code. It's like writing a whole paragraph to say one sentence. It's easy to make mistakes and hard to read when you come back later.
Lambda expressions let you write these small pieces of code quickly and clearly, right where you need them. They are like shorthand notes that do the job without extra fuss.
int CompareByLength(string s1, string s2) { return s1.Length - s2.Length; }
names.Sort(CompareByLength);names.Sort((s1, s2) => s1.Length.CompareTo(s2.Length));
Lambda expressions make your code shorter, easier to read, and faster to write, especially for small tasks.
When filtering a list of products to find only those cheaper than $20, a lambda lets you write the filter condition right inside the method call without creating a separate function.
Manual methods for small tasks are slow and bulky.
Lambdas provide a quick, inline way to write small functions.
This makes code cleaner, easier, and faster to write.
Practice
x => x * 2 represent in C#?Solution
Step 1: Understand lambda syntax
The expressionx => x * 2means a function taking inputxand returningx * 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 BQuick Check:
Lambdax => x * 2doubles input [OK]
- Thinking lambda is a variable
- Confusing lambda with loops
- Assuming lambda is a conditional
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 expressiona + bwithout braces is valid for single expressions. (a, b) => a + b matches this.Final Answer:
(a, b) => a + b -> Option AQuick Check:
Two parameters need parentheses and expression body [OK]
- Omitting parentheses for multiple parameters
- Adding semicolon inside lambda expression
- Using braces without return keyword incorrectly
Func<int, int> square = x => x * x; Console.WriteLine(square(4));
Solution
Step 1: Understand the lambda function
The lambdax => x * xreturns the square of the input number.Step 2: Calculate the output for input 4
4 * 4 = 16, sosquare(4)returns 16.Final Answer:
16 -> Option DQuick Check:
Square of 4 is 16 [OK]
- Confusing multiplication with addition
- Expecting lambda to print instead of return
- Thinking lambda causes syntax error
Func<int, int> add = (x, y) => x + y;
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 CQuick Check:
Func<int, int> means one input, one output [OK]
- Assuming multiple parameters allowed for Func
- Forgetting delegate parameter count rules
- Thinking return keyword is always needed
List<int>.Where()?var numbers = new List<int> {1, 2, 3, 4, 5, 6};
var odds = numbers.Where( /* which lambda? */ );Solution
Step 1: Understand filtering condition
We want to keep odd numbers, so condition isx % 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 AQuick Check:
Filter odds with modulo == 1 [OK]
- Using = instead of == in lambda
- Filtering evens instead of odds
- Confusing division with modulo
Ready to prove it?
Pass the test and this pattern moves unknown
