this的使用

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 是錯誤的。



發佈了6 篇原創文章 · 獲贊 15 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章