国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# this關(guān)鍵字的四種用法

2020-01-24 00:55:03
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了C# this關(guān)鍵字的四種用法,供大家參考,具體內(nèi)容如下

用法一  this代表當(dāng)前實(shí)例,用this.顯式調(diào)用一個(gè)類的方法和成員

namespace Demo{  public class Test  {    private string scope = "全局變量";    public string getResult()    {      string scope = "局部變量";       // 在這里,this代表Test的實(shí)例,所以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實(shí)現(xiàn)原始類型的擴(kuò)展(下一篇詳解)

用法三  通過this實(shí)現(xiàn)索引器,可用于優(yōu)化程序性能(下一篇詳解)

用法四  用this串聯(lián)構(gòu)造函數(shù)

namespace Demo{  public class Test  {    public Test()    {      Console.WriteLine("無參構(gòu)造函數(shù)");    }    // 這里的this()指向的是Test()無參構(gòu)造函數(shù)    // 相當(dāng)于繼承了無參構(gòu)造函數(shù)    public Test(string text) : this()    {      // 程序進(jìn)來后會(huì)先執(zhí)行Test()無參函數(shù),然后繼續(xù)往下邊執(zhí)行      Console.WriteLine(text);      Console.WriteLine("有參構(gòu)造函數(shù)");    }  }  class Program  {    static void Main(string[] args)    {      try      {        Test test = new Test("張三");      }      catch (Exception ex)      {        Console.WriteLine(ex);      }      finally      {        Console.ReadLine();      }    }  }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 嫩江县| 东丰县| 察隅县| 盐池县| 大足县| 临湘市| 麻城市| 若尔盖县| 鄂温| 华宁县| 奉新县| 毕节市| 白沙| 武乡县| 汉阴县| 呈贡县| 朔州市| 绥江县| 南昌县| 江阴市| 龙游县| 辰溪县| 汕尾市| 正蓝旗| 阿瓦提县| 体育| 广东省| 兰州市| 南木林县| 紫阳县| 泸定县| 金平| 武川县| 湖州市| 宣恩县| 菏泽市| 恭城| 泉州市| 镇安县| 湾仔区| 温州市|