C# List ForEach example

In this source code example, we will demonstrate how to use the ForEach method in C#.

The ForEach method performs the specified action on each element of a list.

C# List ForEach example

The following example demonstrates the usage of the ForEach method:
using System;
using System.Collections.Generic;

public class ListDemo
{
    public static void Main(string[] args)
    {
        var fruits = new List<string> { "banana", "mango", "apple", "watermelon" };

        fruits.ForEach(Console.WriteLine);
        fruits.ForEach(fruit => Console.WriteLine(fruit.ToUpper()));
        
    }
}

Output:

banana
mango
apple
watermelon
BANANA
MANGO
APPLE
WATERMELON

Related C# List Examples


Comments