c#中this的作用

1,代表當前類的實例化對象

2,能顯示的調用當前類的構造方法

public class Student{
 public string name;
 public int age;
 public string gender;
 public Student(string name,int age,string gender)
{
    this.name=name;
    this.age=age;
    this.gender=gender;

}
//先是調用構造方法
public Student(string name,string gender):this(name,0,gender)//0爲年齡的默認值
{
//其他操作
}
  
}

注意:

     this和base均爲類的預定義對象,其屬於類的對象,不屬於類,靜態方法和靜態變量屬於類,所以在靜態方法中不能使用this和base

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章