标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 匿名方法 { class Program { public class Book { public string Name { get; set; } public int Year { get; set; } } static void Main(string[] args) { var books = new List<Book> { new Book{Name="C# learning guide",Year=2005}, new Book{Name="C# step by step",Year=2005}, new Book{Name="Java learning guide",Year=2004}, new Book{Name="Java step by step",Year=2004}, new Book{Name="Python learning guide",Year=2003}, new Book{Name="C# in depth",Year=2012}, new Book{Name="Java in depth",Year=2014}, new Book{Name="Python in depth",Year=2013}, }; Console.WriteLine("最近的-----------------------------------"); //匿名方法只在需要的地方定义,定义的时候,使用delegate关键字,后接参数列表,然后跟上用一对花括号包括起来的函数体即可 books.ForEach(delegate(Book B) { Console.WriteLine("Name = {0}, Year = {1}", B.Name, B.Year); }); //对 List 的每个元素执行指定操作。 Console.WriteLine("111-----------------------------------"); Console.ReadKey(); } } }
详细参考:http://www.cnblogs.com/daxnet/archive/2008/11/12/1687011.html
标签:
原文地址:http://www.cnblogs.com/why1115/p/5399233.html