ForEach
class Program
{
static void Main()
{
int index = 0;
List<String> names = new List<String>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
Console.WriteLine();
Console.WriteLine("ForEach " + index);
index++;
// Display the contents of the list using the Print method.
names.ForEach(Print);
Console.WriteLine();
Console.WriteLine("ForEach "+index);
index++;
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach(delegate (String name)
{
Console.WriteLine(name);
});
Console.WriteLine();
Console.WriteLine("ForEach " + index);
index++;
names.ForEach(item =>
{
Console.WriteLine(item);
});
Console.ReadKey();
}
private static void Print(string s)
{
Console.WriteLine(s);
}
}