Use Action to print out items in a list in CSharp

Description

The following code shows how to use Action to print out items in a list.

Example


using System;//from  w w  w.  ja v  a  2 s .  c om
using System.Collections.Generic;
using System.ComponentModel;

class Film
{
    public string Name { get; set; }
    public int Year { get; set; }
    public override string ToString()
    {
        return string.Format("Name={0}, Year={1}", Name, Year);
    }
}

class MainClass
{
    static void Main()
    {
        var films = new List<Film>
        {
            new Film {Name="J", Year=1975},
            new Film {Name="H", Year=2000},
            new Film {Name="T", Year=1995}
        };

        Action<Film> print = film => Console.WriteLine(film);
        Console.WriteLine("All films");
        films.ForEach(print);                                
        Console.WriteLine();

    }        
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor