Mario Schneider - Software
 
... praktische Tools für jedermann!
software.marioschneider-online.de 
 

List.Foreach()

Code-Snippet

Dot Net (C#): List.ForEach() Methode

Verwenden der ForEach()-Methode einer Generic-List:

static void Main()
    {
        List names = new List();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");

        // Display the contents of the list using the Print method.
        names.ForEach(Print);

        // 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);
        });
    }

    private static void Print(string s)
    {
        Console.WriteLine(s);
    }

Generisch als Erweiterungsmethode für IEnumerable:

public static class IEnumerableExtension
{
    public static void PrintItem(this T item)
    {
        Console.WriteLine(item.ToString());
    }

    public static void PrintAllItems(this IEnumerable enumerable)
    {
        foreach (T item in enumerable)
        {
            PrintItem(item);
        }
    }
}