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

首頁 > 編程 > C# > 正文

C# this關鍵字的四種用法

2019-10-29 21:18:09
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C# this關鍵字的四種用法,供大家參考,具體內容如下

用法一  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實現原始類型的擴展(下一篇詳解)

用法三  通過this實現索引器,可用于優化程序性能(下一篇詳解)

用法四  用this串聯構造函數

 

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

C#,this,關鍵字

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舟山市| 金坛市| 鹿邑县| 铅山县| 临颍县| 沈丘县| 时尚| 凌海市| 临洮县| 平和县| 班戈县| 齐齐哈尔市| 安庆市| 阳江市| 舟曲县| 保山市| 贺州市| 顺平县| 永善县| 剑河县| 嘉鱼县| 宜君县| 黔东| 秦安县| 醴陵市| 岢岚县| 易门县| 英吉沙县| 定州市| 泸西县| 读书| 庆城县| 西盟| 吉林市| 玉屏| 桃园市| 芦山县| 静安区| 阿坝县| 温宿县| 上林县|