C# this关键字

使用方式之一:

this,在构造函数中使用。

当使用构造函数的重载时,可使用this关键字。

 1         //构造函数-重载
 2          public Student(int id, string name, int age, int math)
 3         {
 4             this.Id = id;
 5             this.Name = name;
 6             this.Age = age;
 7             this.Math = math;
 8         }
 9 
10         //this关键字,具有最少参数的构造器调用具有最多参数的构造器
11         public Student(int id, int math)
12             :this(id,"NULL",0, 100)
13         {
14         }

当调用 public Student(int id, int math)(命名为构造函数1)构造函数的时候,因为使用了this关键字,所以执行的顺序是,当执行构造函数1的时候,系统会直接把构造函数1中的参数,传递给 public Student(int id, string name, int age, int math)(命名为构造函数2),然后在构造函数2中,把值赋给属性。

原文地址:https://www.cnblogs.com/KTblog/p/4437643.html