萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C#中this的用法集錦

C#中this的用法集錦

   本文給大家匯總介紹了C#中的幾種this用法,相信大家應該有用過,但你用過幾種?以下是個人總結的this幾種用法,歡迎大家拍磚,廢話少說,直接列出用法及相關代碼。

  this 關鍵字引用類的當前實例,還可用作擴展方法的第一個參數的修飾符。下面就針對this的四種用法,做簡單的總結。

  首先,我們分別創建User和VIP兩個C#類

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;   namespace AppConsole {   /// <summary> /// 普通用戶 /// </summary> public class User { /// <summary> /// 全局變量 /// </summary> Dictionary<string, object> dictInfo = null;   /// <summary> /// 構造器 /// </summary> public User() { dictInfo = new Dictionary<string, object>(); }   /// <summary> /// 構造函數重載 /// </summary> /// <param name="UserID"></param> /// <param name="UserName"></param> public User(int UserID, string UserName) { this.UserName = UserName; this.ID = UserID; }   /// <summary> /// this,第【1】種用法,索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public object this[string name] { get { return dictInfo[name]; } set { dictInfo[name] = value; } }     /// <summary> /// 編號 /// </summary> public int ID { get; set; }     /// <summary> /// 用戶名 /// </summary> public string UserName { get; set; }   /// <summary> /// this第【2】種用法,當做參數傳遞 /// </summary> public void Said() { new VIP().Say(this); }   }   /// <summary> /// 會員 /// </summary> public class VIP : User {     /// <summary> /// 積分 /// </summary> public int integral { get; set; }   /// <summary> /// 構造函數 /// </summary> public VIP() { ID = 520; integral = 1000; }   /// <summary> /// this第【3】種用法,通過this()調用無參構造函數 /// </summary> /// <param name="UserName"></param> public VIP(string UserName) : this() { this.UserName = UserName; }   /// <summary> /// 構造函數重載 /// </summary> /// <param name="UserID"></param> /// <param name="UserName"></param> public VIP(int UserID, string UserName) : base(UserID, UserName) {   }   /// <summary> ///Say方法 /// </summary> /// <param name="user"></param> public void Say([LCQAttribute] User user) { Console.WriteLine(string.Format("嗨,大家好!我的編號是{0},大家可以叫我{1}!", user.ID, user.UserName)); }   }   /// <summary> /// 靜態類,來擴展User類 /// </summary> public static class Helper {   /// <summary> /// 第【4】種用法: this擴展User類 /// </summary> /// <param name="user"></param> public static void Sing(this User user) { Console.WriteLine(string.Format("嗨,大家好!我的編號是{0},大家可以叫我{1}!", user.ID, user.UserName)); } }   /// <summary> /// 特性類:指定特性僅適用於方法和方法的參數 /// </summary> [System.AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter)] public class LCQAttribute : System.Attribute {   } }

  this第【1】種用法,索引器

  ?

1 2 3
copyright © 萬盛學電腦網 all rights reserved