1.this代表當前類的實例對象
public class Test { public string Name{get;set;} public void TestChange(string strName) { this.Name=strName; } }
2.搭配構造函數:a.直接當成參數傳遞 b.構造函數執行順序
public class Test { public Test() { Console.WriteLine("無參構造函數"); } // this()對應無參構造方法Test() // 先執行Test(),后執行Test(string text) public Test(string text) : this() { Console.WriteLine(text); Console.WriteLine("有參構造函數"); } }
3.類的索引器:參數可以是int,也可以是string等其他類型
public class Test { public string Name{get;set;} //索引器 []括號里面可以是string,int,array等 public string this[string index] { if(index=="xxx") { return "xxx"; } else { return "ccc"; } } }
4.類的擴展方法;注意:類必須是靜態,方法也是靜態,格式如下
public static class Test { //this 放在參數前,且該方法對應string //改變參數類型就可以應對其他類型 public static string ExpandString(this string name) { return name+name; } }