This article gives you a summary of C # in several of this usage, I believe you should be useful, but you have used several? The following is a personal summary of this several uses, welcome everyone to Pat Bricks, nonsense less, directly listed usage and related code.
The This keyword refers to the current instance of the class and can also be used as a modifier to the first parameter of the extension method. Here's a quick summary of the four uses for this.
First, we create the user and VIP two C # classes separately
?
| 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 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 5, 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 11 9 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>///Normal User///</summary> public class User {///<summary >///global variable///</summary> dictionary<string, object> dictinfo = null; ///<summary>///builder///</summary> public User () {dictinfo = new dictionary<string, object> (); } ///<summary>///constructor overload///</summary>///<param name= "UserID" ></param>///<param Name= "UserName" ></param> public User (int UserID, string UserName) {this. UserName = UserName; This.id = UserID; } ///<summary>///This, the "1" usage, indexer///</summary>///<param name= "name" ></param>/// ;returns></returns> public Object This[string name] {get {return dictinfo[name];} set {Dictinfo[name] = value ; } ///<summary>///number///</summary> public int ID {get; set;} ///<summary>///user name///</summary> public string UserName {get; Set } ///<summary>///This "2" usage as a parameter passing///</summary> public void Said () {new VIP (). Say (this); } } ///<summary>///member///</summary> public class Vip:user { ///<summary& Gt Integral///</summary> public int integral {get; set;} ///<summary>///Constructor///</summary> Pub Lic VIP () {ID = 520; integral = 1000;} ///<summary>///This article "3" usage, call the parameterless constructor via this ()///</summary> <param name= "UserName" ></param> public VIP (String UserName): this () {this. UserName = UserName; } ///<summary>///constructor overload///</summary>///<param name= "UserID" ></param>///<param N Ame= "UserName" ></param> public VIP (int UserID, string UserName): Base (UserID, UserName) { } ///&L T;summary>///say Method///</summary>///<param name= "user" ></param> public void Say ([lcqattribute] User user) {Console.WriteLine ( String. Format ("Hi, everyone!") My number is {0}, you can call me {1}! ", User.ID, user. UserName)); } } ///<summary>///static class to extend the user class///</summary> public static class Helper { ///<su Mmary>///"4" usage: This extends the user class///</summary>///<param name= "user" ></param> public static void Sin G (this user user) {Console.WriteLine (string. Format ("Hi, everyone!") My number is {0}, you can call me {1}! ", User.ID, user. UserName)); } ///<summary>///Attribute class: Specifies that attributes apply only to the parameters of methods and methods///</summary> [System.AttributeUsage ( AttributeTargets.Method | Attributetargets.parameter)] public class LCQAttribute:System.Attribute { }} |
This is the "1" kind of usage, indexers
?
| 1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
<summary>///global variable///</summary> dictionary<string, object> dictinfo = null; <summary>///This, first "1" usage, indexer///</summary>///<param name= "name" ></param>///< Returns></returns> public Object This[string name] {get {return dictinfo[name];} set {Dictinfo[name] = value; } } |
This is the "2" kind of usage, as parameter passing
?
| 1 2 3 4 5 6 7 |
<summary>///This "2" usage, as parameter passing///</summary> public void Said () {new VIP (). Say (this); } |
This is the "3" method of using this () to invoke the parameterless constructor
?
| 1 2 3 4 5 6 7 8 9 |
<summary>///This "3" usage, using this () to invoke the parameterless constructor///</summary>///<param name= "UserName" ></param > Public VIP (String UserName): this () {this. UserName = UserName; } |
This article "4" Usage: Extend the user class
?
| 1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
<summary>///Static class to extend the user class///</summary> public static class Helper {///<summary>///the "4" Usage: This extends the user class///</summary>///<param name= "user" ></param> public static void Sing (this user user) {Con Sole. WriteLine (String. Format ("Hi, everyone!") My number is {0}, you can call me {1}! ", User.ID, user. UserName)); } } |
Finally, the console tests
?
| 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 |
<summary>///Main program entry///</summary>///<param name= "args" ></param> static void Main (string[] A RGS) {//0> declares entity user user = new user (); user.id = 1; user. UserName = "Lichaoqiang"; The first "one" usage: This is used as the indexer public object this[string name]{...} user["UserID" = 1; Console.WriteLine ("The First" one "usage: This is used as an indexer"); "Two" usage: this is used as a parameter to pass user. Say (this); Console.WriteLine ("The Second" usage: This is used as a parameter pass "); User. Said (); The use of the "three": this () public vip:this () {} VIP VIP = new VIP ("Yezi"); Vip. Said (); Console.WriteLine ("The Third" usage: this ()); The use of the "four": This extends the VIP class public static Sing (this user) {...} Console.WriteLine ("The" Four "usage: This expands the VIP class"); User. Sing (); Console.read (); } |
Schematic of the final result
The above mentioned is the entire content of this article, I hope you can enjoy.