1. 程式人生 > >C#中this關鍵字的使用

C#中this關鍵字的使用



this的兩個作用:
1、代表當前類的物件;
2、在類當中顯示的呼叫本類的建構函式 例如以下兩個建構函式中:
 public Students(string name,char gender,int age,int chinese,int math,int english)
        {
            this.Name = name;
            this.Gender = gender;
            this.Age = age;
            this.Chinese = chinese;
            this.Math = math;
            this.English = english;

        }
        //為避免建構函式程式碼的冗餘,我們可以使用this關鍵字
        public Students(string name,char gender,int age):this(name,gender,age,0,0,0)    //沒有的引數用任意數值代替,但是必須要同類型資料
        {
            //this.Name = name;
            //this.Gender = gender;
            //this.Age = age;

        }