How to perform a specified action on each element of the List in C#?

In C#, you can perform a specified action on each element of a List<T> using the ForEach() method. This method executes a delegate function on every element in the list, making it convenient for applying transformations, calculations, or operations without manually iterating through the collection.

Syntax

Following is the syntax for using List<T>.ForEach() method −

list.ForEach(Action<T> action);

Where action is a delegate that takes one parameter of type T and returns void.

Parameters

  • action − The Action<T> delegate to perform on each element of the list.

Using ForEach() with a Method

You can pass a method as an action to be performed on each list element −

using System;
using System.Collections.Generic;

public class Demo {
   public static void MultiplyByTen(int s) {
      s = s * 10;
      Console.WriteLine(s);
   }

   public static void Main(String[] args) {
      List<int> list = new List<int>();
      list.Add(25);
      list.Add(50);
      list.Add(75);
      list.Add(100);
      list.Add(200);
      list.Add(250);
      list.Add(275);
      list.Add(300);

      Console.WriteLine("Original List...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }

      Console.WriteLine("\nAfter multiplying each element by 10...");
      list.ForEach(MultiplyByTen);
   }
}

The output of the above code is −

Original List...
25
50
75
100
200
250
275
300

After multiplying each element by 10...
250
500
750
1000
2000
2500
2750
3000

Using ForEach() with Lambda Expressions

Lambda expressions provide a more concise way to define the action inline −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Diana" };

      Console.WriteLine("Original Names...");
      names.ForEach(name => Console.WriteLine(name));

      Console.WriteLine("\nNames in uppercase...");
      names.ForEach(name => Console.WriteLine(name.ToUpper()));

      Console.WriteLine("\nNames with greeting...");
      names.ForEach(name => Console.WriteLine($"Hello, {name}!"));
   }
}

The output of the above code is −

Original Names...
Alice
Bob
Charlie
Diana

Names in uppercase...
ALICE
BOB
CHARLIE
DIANA

Names with greeting...
Hello, Alice!
Hello, Bob!
Hello, Charlie!
Hello, Diana!

Using ForEach() for Mathematical Operations

Here's an example that performs subtraction on each element −

using System;
using System.Collections.Generic;

public class Demo {
   public static void SubtractFifty(int s) {
      int result = s - 50;
      Console.WriteLine($"{s} - 50 = {result}");
   }

   public static void Main(String[] args) {
      List<int> numbers = new List<int> { 25, 50, 75, 100, 200, 250, 275, 300 };

      Console.WriteLine("Subtracting 50 from each element...");
      numbers.ForEach(SubtractFifty);
   }
}

The output of the above code is −

Subtracting 50 from each element...
25 - 50 = -25
50 - 50 = 0
75 - 50 = 25
100 - 50 = 50
200 - 50 = 150
250 - 50 = 200
275 - 50 = 225
300 - 50 = 250

Comparison of Approaches

Approach Advantages Best Used For
Method Reference Reusable, clear separation of concerns Complex operations used multiple times
Lambda Expression Concise, inline definition Simple operations used once
Traditional foreach loop More control, can break/continue Complex logic requiring flow control

Conclusion

The List<T>.ForEach() method provides an elegant way to perform actions on every element in a list. Use method references for reusable operations and lambda expressions for simple inline actions, making your code more readable and functional in style.

Updated on: 2026-03-17T07:04:36+05:30

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements