现在的位置: 首页 > 综合 > 正文

this的使用

2012年12月22日 ⁄ 综合 ⁄ 共 333字 ⁄ 字号 评论关闭

this关键字引用类的当前实例,还可用作扩展方法的第一个参数的修饰符。

  • 限定被相似的名称隐藏的成员,例如:

public Employee(string name, string alias)
{
    // Use this to qualify the fields, name and alias:
    this.name = name;
    this.alias = alias;
}

  • 将对象作为参数传递到其他方法,例如:

CalcTax(this);

  • 声明索引器,例如:

public int this[int param]
{
    get { return array[param]; }
    set { array[param] = value; }
}

由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有 this 指针。在静态方法中引用
this
是错误的。

抱歉!评论已关闭.