C#几个不常用的关键字

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]

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值