| this操作數(shù)代表的是指向此對(duì)象的參考指針。也就是說,在建立對(duì)象的實(shí)體后,我們就可以使用this來存取到此對(duì)象實(shí)體。另外,this操作數(shù)也可以用來解決名稱相同的問題。
 需要注意的是:靜態(tài)方法中不能使用this。
 示例如下: Example 1:this操作數(shù)用來解決名稱相同的問題。
 
class Employee{
 …
 public void SetEmpName(string EmpName)
 {
 EmpName = EmpName;//問題語句
 }
 private string EmpName; //Employee的成員變量
 }
 上述代碼的本意是要將SetEmpName的傳入?yún)?shù)EmpName的值指定給Employee類的成員變量EmpName。但是這樣做并沒有成功,因?yàn)橄到y(tǒng)并不知道上述問題語句中的第一個(gè)EmpName指的是類成員。
 解決方法是將問題語句修改如下:
 
this.EmpName = EmpName; Example 2:使用this操作數(shù)返回目前對(duì)象的參考。
 
class Employee{
 …
 public Employee SetEmpName(string EmpName)
 {
 this.EmpName = EmpName;
 return this;
 }
 public Employee SetEmpID(string EmpID)
 {
 mstrEmpID = EmpID;
 return this;
 }
 private string EmpName;
 private string mstrEmpID = “”;
 }
 這樣,我們可以使用如下語句來設(shè)置員工代號(hào)和員工名稱了: 
public class HumanResource {public static int Main(){
 Employee e1 = new Employee();
 e1.SetEmpID(”001″).SetEmpName(”William”);//示例語句
 return 0;
 }
 }
 Example 3(這 |