The ForEach method of the List<T>executes an operation for every object which is stored in the list.
Example 1: Simple List ForEach example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Program { static void Main(string[] args) { List<int> numbers = new List<int>() { 10, 20, 30, 40, 50, 60, 70 }; numbers.ForEach(x => Console.WriteLine(x)); Console.ReadLine(); } } |
Example 2: Using of C# List ForEach with Custom Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Program { private static void Println(string s) { Console.WriteLine(s); } static void Main(string[] args) { List<String> names = new List<String>(); names.Add("Table"); names.Add("Lion"); names.Add("River"); names.Add("Banana"); names.ForEach(Println); Console.ReadLine(); } } } |
Example 3: Using of C# List ForEach with delegate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Program { static void Main(string[] args) { List<String> names = new List<String>(); names.Add("Table"); names.Add("Lion"); names.Add("River"); names.Add("Banana"); names.ForEach(delegate (String name) { Console.WriteLine(name); }); Console.ReadLine(); } } |
Example 4: Using of C# List ForEach with Lambda
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Program { static void Main(string[] args) { List<String> names = new List<String>(); names.Add("Table"); names.Add("Lion"); names.Add("River"); names.Add("Banana"); names.ForEach(x => { Console.WriteLine(x); }); Console.ReadLine(); } } |
Example 5: Using with Action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Program { static void Main(string[] args) { List<Action> actions = new List<Action>(); List<int> numbers = new List<int>() { 10, 20, 30, 40, 50, 60, 70 }; numbers.ForEach(number => actions.Add(() => Console.WriteLine(number))); foreach (Action action in actions) action(); Console.ReadLine(); } } |
Example 6:
class Book
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Book { string title; string genre; int price; public string Title { get => title; set => title = value; } public string Genre { get => genre; set => genre = value; } public int Price { get => price; set => price = value; } public Book(string t, string g, int p) { title = t; genre = g; price = p; } } |
class Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { List<Book> Books = new List<Book>() { new Book("Broken hearts","Romance",12), new Book("Nothing in my life","Guide",2), new Book("Love for love", "Romance", 10), new Book("Spain", "Travel", 8), new Book("Fruits", "Cookbooks", 25) }; Books.ForEach(delegate (Book b) { Console.WriteLine("Name:{0}",b.Title); Console.WriteLine("Genre:{0}", b.Genre); Console.WriteLine("Price:{0}", b.Price); Console.WriteLine(); }); Console.ReadLine(); } } |
Example 7: When you use a normal foreach
statement, you can’t modify items while iterating over the collection. But with List.ForEach
you can do it.
C# ForEach, update Romance values with by Drama
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Program { static void Main(string[] args) { List<Book> Books = new List<Book>() { new Book("Broken hearts","Romance",12), new Book("Nothing in my life","Guide",2), new Book("Love for love", "Romance", 10), new Book("Spain", "Travel", 8), new Book("Fruits", "Cookbooks", 25) }; Books.ForEach(delegate (Book b) { Console.WriteLine("Name:{0}", b.Title); Console.WriteLine("Genre:{0}", b.Genre); Console.WriteLine("Price:{0}", b.Price); Console.WriteLine(); }); Books.ForEach(delegate (Book x) { if(x.Genre=="Romance") { x.Title = "Drama"; } }); Books.ForEach(delegate (Book b) { Console.WriteLine("Name:{0}", b.Title); Console.WriteLine("Genre:{0}", b.Genre); Console.WriteLine("Price:{0}", b.Price); Console.WriteLine(); }); Console.ReadLine(); } } |
Source:
https://stackoverflow.com/questions/225937/foreach-vs-somelist-foreach
https://msdn.microsoft.com/tr-tr/library/bwabdf9z(v=vs.110).aspx