List<T>ForEach()
作者:PaulLeder 日期:2010-06-25
引用内容using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<DemoEntiy> list = new List<DemoEntiy>();
list.Add(new DemoEntiy(1, "paul"));
list.Add(new DemoEntiy(2, "leder"));
list.Add(new DemoEntiy(3, "china"));
//.NET 2.0实现
System.Text.StringBuilder sb = new System.Text.StringBuilder();
list.ForEach(delegate(DemoEntiy demo)
{
sb.Append(demo.Name + ",");
});
Response.Write(string.Format("Result:{0}<br/>", sb.ToString().TrimEnd(',')));
//.NET 3.0以上 LINQ实现
sb.Length = 0;
list.ForEach(c => sb.Append(c.Name + ","));
Response.Write(string.Format("LINQ Result:{0}<br/>", sb.ToString().TrimEnd(',')));
}
}
public class DemoEntiy
{
public DemoEntiy(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<DemoEntiy> list = new List<DemoEntiy>();
list.Add(new DemoEntiy(1, "paul"));
list.Add(new DemoEntiy(2, "leder"));
list.Add(new DemoEntiy(3, "china"));
//.NET 2.0实现
System.Text.StringBuilder sb = new System.Text.StringBuilder();
list.ForEach(delegate(DemoEntiy demo)
{
sb.Append(demo.Name + ",");
});
Response.Write(string.Format("Result:{0}<br/>", sb.ToString().TrimEnd(',')));
//.NET 3.0以上 LINQ实现
sb.Length = 0;
list.ForEach(c => sb.Append(c.Name + ","));
Response.Write(string.Format("LINQ Result:{0}<br/>", sb.ToString().TrimEnd(',')));
}
}
public class DemoEntiy
{
public DemoEntiy(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
.NET 2.0是通过委托实现,.NET 3.0以上是用LINQ,LINQ其实也就是一种特殊的委托。所以LINQ总是会有延时。
评论: 0 | 引用: 0 | 查看次数: 93
发表评论
上一篇
下一篇

文章来自:
Tags:
相关日志:






