yield关键字与数据源
今天遇到的问题,记下来,主要是想回忆一下IEnumerable也可以做数据源。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class YieldBinc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = Retval(10, 5);
DataBind();
}
}
private IEnumerable Retval(int intNum, int intExp)
{
long intResult = 1;
for (int i = 0; i < intExp; i++)
{
intResult *= intNum;
yield return intResult;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class YieldBinc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = Retval(10, 5);
DataBind();
}
}
private IEnumerable Retval(int intNum, int intExp)
{
long intResult = 1;
for (int i = 0; i < intExp; i++)
{
intResult *= intNum;
yield return intResult;
}
}
}