this关键字的作用
1.代表当前对象
2.在类中显式的调用本类的构造函数
语法为 :this()
//构造函数
public Student(string name,int age,char gender,double score)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Score = score;
}
//public Student(string name, int age, char gender)
//{
// this.Name = name;
// this.Age = age;
// this.Gender = gender;
//}
//下面的构造函数调用了上面的构造函数,没有的参数随便传一个数即可,这样可以避免代码冗余
public Student(string name, double score)
: this(name, 0, 'a', score)
{ }
析构函数
//析构函数
~Student()
{
Console.WriteLine("我是析构函数");
}
当用户创建类的对象是调用构造函数,而当该对象已经调用完毕时,使用析构函数。析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。可以在析构函数中进行资源的释放。
析构函数是自动执行的,无法手动调用。