C# > Advanced C# > Delegates and Events > Lambda Expressions
Simple Lambda Expression with Delegate
This snippet demonstrates a basic lambda expression used with a delegate to perform a simple calculation. It showcases the concise syntax of lambda expressions and their integration with delegates.
Code Example
This code defines a delegate `NumberProcessor` which takes an integer as input and returns an integer. A lambda expression `x => x * x` is then assigned to this delegate. This lambda expression takes an input `x` and returns its square. The `square` delegate is then invoked with the value 5, and the result is printed to the console.
using System;
public class LambdaExample
{
// Define a delegate that takes an integer and returns an integer
public delegate int NumberProcessor(int number);
public static void Main(string[] args)
{
// Lambda expression that squares a number
NumberProcessor square = x => x * x;
// Use the delegate to process a number
int result = square(5);
Console.WriteLine($"The square of 5 is: {result}"); // Output: The square of 5 is: 25
}
}
Concepts Behind the Snippet
Delegates are type-safe function pointers, allowing you to pass methods as arguments to other methods. Lambda expressions provide a concise way to define anonymous functions that can be assigned to delegates. The `x => x * x` is a lambda expression; `x` is the input parameter, and `x * x` is the expression that calculates the return value.
Real-Life Use Case
Lambda expressions are commonly used in LINQ queries for filtering, sorting, and projecting data. They are also useful in event handling and asynchronous programming where you need to define small, inline functions.
Best Practices
Keep lambda expressions short and readable. For complex logic, consider extracting the logic into a separate named method. Use descriptive parameter names to improve code clarity.
When to use them
Lambda expressions are best suited for simple, concise functions that are used inline. They are particularly useful when working with delegates, events, and LINQ queries where you need to define functions on the fly.
Interview Tip
Be prepared to explain the difference between delegates and lambda expressions. Delegates are type-safe function pointers, while lambda expressions are a concise way to create anonymous functions that can be assigned to delegates.
FAQ
-
What is a delegate?
A delegate is a type that represents references to methods with a particular parameter list and return type. It's like a function pointer in C, but type-safe. -
What is a lambda expression?
A lambda expression is an anonymous function that can be treated as a value and passed around. It provides a concise way to define simple functions inline. -
What is the difference between Action and Func delegates?
Action delegates represent methods that return void, while Func delegates represent methods that return a value. Actiontakes a parameter of type T and returns void. Func takes a parameter of type T and returns a value of type TResult.