Do action on each element of the List in CSharp
Description
The following code shows how to do action on each element of the List.
Example
using System;/*from w ww . j a v a2s . c o m*/
using System.Collections.Generic;
class Program
{
static void Main()
{
List<String> names = new List<String>();
names.Add("A");
names.Add("B");
names.Add("C");
names.Add("D");
names.ForEach(Print);
names.ForEach(delegate(String name)
{
Console.WriteLine(name);
});
}
private static void Print(string s)
{
Console.WriteLine(s);
}
}
The code above generates the following result.