C# Example: Lambda Expression with ForEach() on Generic List
In the world of C#, lambda expressions are a powerful tool for simplifying code and making it more efficient. One particular use case for lambda expressions is with the ForEach() method on generic lists. In this article, we will explore how to use lambda expressions with ForEach() on a generic list in C#.
First, let's define what a lambda expression is. A lambda expression is an anonymous function that can be used as a parameter for a method or assigned to a variable. It is a concise way to write a function without having to explicitly define a method. With lambda expressions, you can write code that is more readable and maintainable.
Now, let's take a look at how to use lambda expressions with the ForEach() method on a generic list. The ForEach() method is used to loop through each element in a list and perform an action on it. Traditionally, this is done using a foreach loop, but with lambda expressions, we can achieve the same result with less code.
To use lambda expressions with ForEach() on a generic list, we first need to define the list. For this example, let's create a list of integers.
```
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
```
Next, we can use the ForEach() method on the list and pass in a lambda expression as a parameter. The lambda expression will define the action we want to perform on each element in the list.
```
numbers.ForEach(num => Console.WriteLine(num * 2));
```
In this example, the lambda expression takes in a parameter called "num" and multiplies it by 2. Then, the result is printed to the console. This code is equivalent to writing a foreach loop and performing the same action on each element in the list.
Lambda expressions can also be used to filter out specific elements in a list before performing an action on them. Let's modify our previous example to only print the even numbers in the list.
```
numbers.ForEach(num => {
if (num % 2 == 0)
Console.WriteLine(num);
});
```
In this example, the lambda expression uses an if statement to check if the current element is even before printing it to the console. This shows how lambda expressions can be used to add conditional logic to our code.
Another advantage of using lambda expressions with ForEach() on a generic list is that it allows us to easily perform multiple actions on each element. Let's say we want to both print the element and add it to a new list. We can do this by using a lambda expression with multiple statements enclosed in curly braces.
```
List<int> newNumbers = new List<int>();
numbers.ForEach(num => {
Console.WriteLine(num);
newNumbers.Add(num);
});
```
In this example, the lambda expression prints the element and then adds it to the new list. This is a more concise way to achieve the same result compared to writing a foreach loop and performing both actions within it.
In conclusion, lambda expressions are a powerful tool in C# and can greatly simplify code. When used with the ForEach() method on a generic list, they allow us to perform actions on each element in the list with less code and more readability. So next time you need to loop through a list, consider using lambda expressions with For