C# this 关键字解析

C# this 关键字解析 this 表示当前对象的引用一.this 是什么class Person { public string name; public void SetName(string name) { this.name name; } }调用时Person p new Person(); p.SetName(张三);此时this→pthis.name→p.name二.this 的本质内存角度this 就是当前对象在堆中的地址三.构造函数中的 thisclass Person { public string name; public Person(string name) { this.name name; } }作用区分成员变量区分参数变量四.示例class Student { public string name; public Student(string name) { this.name name; } public void Print() { Console.WriteLine(this.name); } } class Program { static void Main() { Student s new Student(李四); s.Print(); } }内存总结s→ 栈Student对象 → 堆this→ 指向堆中的对象五.总结在 C# 中类是模板实例是对象对象在堆中引用在栈中this 是当前对象的“身份证”。