1、[b][size=medium]yield[/size][/b]
yield关键字会告诉编译器当前的函数是在一个循环内部,编译器会相应生成一个执行它在循环体内部所表示行为的类,yield和return关键字一起用于为枚举器对象提供返回值,比如说:在foreach内部的每一次循环内,yield关键字用于终止当前循环:
以下为MSDN示例代码[url=http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx]http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx[/url]
输出:
2
4
8
16
32
64
128
256
===================================
2、[size=large]using[/size]
定义一个范围,在范围外的对象将会被回收:
参考链接:[url=http://hatim.indexdev.net/2009/12/08/10-not-so-well-known-keywords-in-c/]http://hatim.indexdev.net/2009/12/08/10-not-so-well-known-keywords-in-c/[/url]
yield关键字会告诉编译器当前的函数是在一个循环内部,编译器会相应生成一个执行它在循环体内部所表示行为的类,yield和return关键字一起用于为枚举器对象提供返回值,比如说:在foreach内部的每一次循环内,yield关键字用于终止当前循环:
以下为MSDN示例代码[url=http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx]http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx[/url]
using System.Collections;
public classList
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while(counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
输出:
2
4
8
16
32
64
128
256
===================================
2、[size=large]using[/size]
定义一个范围,在范围外的对象将会被回收:
public static List<updatetime_model> UpdateTime(string datetime)
{
SqlParameter[] par = new SqlParameter[] {
new SqlParameter("@time",datetime)
};
List<updatetime_model> time = null;
[color=red]using (SqlDataReader r = SqlHelper.ExecuteReader("UpdateDate_GetTime",par))
{
while (r.Read())
{
if (time == null)
{
time = new List<updatetime_model>();
}
time.Add(ConvertTime(r));
}
if (r.NextResult() && r.Read())
{
lastdt = r.GetString(0);
}
}[/color]
return time;
}
参考链接:[url=http://hatim.indexdev.net/2009/12/08/10-not-so-well-known-keywords-in-c/]http://hatim.indexdev.net/2009/12/08/10-not-so-well-known-keywords-in-c/[/url]