Here is the difference between Lambdas and Delegates in C#:
Lambdas
- Lambdas are anonymous functions that can be passed as arguments to methods or assigned to variables.
- They are defined using the => operator, which separates the lambda's parameters from its body.
- Lambdas can capture variables from their enclosing scope, allowing them to access and modify variables outside of their own body.
- Lambdas are often used with LINQ queries and methods like
Where
, Select
, OrderBy
, etc.
- Example:
Func<int, int> square = x => x * x;
int result = square(5); // result = 25
Delegates
- Delegates are types that represent methods with a particular signature (return type and parameter types).
- They are declared with a method signature and can be assigned to variables or p...