关键字篇(待补充)

using:

  using 关键字有两个主要用途:

  作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型。
    1.指定命名空间,例:using System;
    2.使用别名,例:using AliasToMyClass = NameSpace1.MyClass;
  作为语句,用于定义一个范围,在此范围之外释放对象。
    例:  

using (Font font1 = new Font("Arial", 10.0f))
{//code
}
//可以在 using 语句中声明对象(如上所示),或者在 using 语句之前声明对象
Font font2 = new Font("Arial", 10.0f);
using (font2)
{
// use font2
}
//可以有多个对象与 using 语句一起使用,但是必须在 using 语句内部声明这些对象,如下所示:
using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}
  注:using 语句允许程序员指定使用资源的对象应当何时释放资源。为 using 语句提供的对象必须实现 IDisposable 接口。此接口提供了 Dispose 方法,该方法将释放此对象的资源。可以在到达 using 语句的末尾时,或者在该语句结束之前引发了异常并且控制权离开语句块时,退出 using 语句。
  可参考以下两篇文章:
     http://msdn.microsoft.com/zh-cn/library/zhdeatwt(v=vs.80).aspx
     http://www.cnblogs.com/windsails/archive/2004/09/12/42444.html


class/interface

delegate
event

out

ref


const/static/readonly

  const与readonly的区别如MSDN所写:

      The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a  constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:


virtual

try/catch/finally

public/private/protected

for/in/foreach/goto/do/while/switch/case

yield

  概念:

    yield 关键字向编译器指示它所在的方法是迭代器块。 编译器生成一个类来实现迭代器块中表示的行为。 在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值。 这是一个返回值,例如,在 foreach 语句的每一次循环中返回的值。 yield 关键字也可与 break 结合使用,表示迭代结束。

  例:  

yield return <expression>;
yield break;


posted @ 2012-03-08 16:19  蜗牛的未来  阅读(289)  评论(0编辑  收藏  举报