this關鍵字代表當前實例,我們可以用this.來調用當前實例的成員方法,變量,屬性,字段等
namespace Demo
{
public class Test
{
private string scope = "全局變量";
public string getResult()
{
string scope = "局部變量";
// 在這裡,this代表Test的實例,所以this.scope指向的是全局變量,scope所訪問的是局部變量
return this.scope + "-" + scope;
}
}
class Program
{
static void Main(string[] args)
{
try
{
Test test = new Test();
Console.WriteLine(test.getResult());
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.ReadLine();
}
}
}
}
下一篇,分享this關鍵字之為原始類型擴展方法