In C #, how many times have you used this? The following are my summary of the usage of this. You are welcome to refer to the usage and related instructions.Code.
- This usage 1: Restrict hidden members with similar names
/// <Summary> ////****************************** * *********** ///* This usage 1: restrict the member to be hidden by similar names */////************************** * *************** // </Summary> // <Param name = "name"> </param> Public person (string name, string sex) {This. name = Name; this. sex = sex ;}
- This usage 2: passing objects as parameters to other methods
/// <Summary> /// abstract description of person /// </Summary> public class person {// <summary> /// name /// </Summary> Public string name {set; get ;} /// <summary> ////****************************** * ************ ///* This usage 2: pass the object as a parameter to another method */////************************** * **************** // </Summary> Public void showname () {helper. printname (this );}} /// <summary> /// auxiliary class /// </Summary> Public static class helper {// <summary> /// print the name of the person </Summary> /// <Param name = "person"> </param> Public static void printname (person) {httpcontext. current. response. write ("name:" + person. name + "<br/> ");}}
- This usage 3: declare the Indexer
/// <Summary> /// other attributes /// </Summary> Public namevaluecollection ATTR = new namevaluecollection (); /// <summary> ////*************************///// * This usage 3: declare the indexer * // ****************************** // </ summary> /// <Param name = "key"> </param> // <returns> </returns> Public String This [String key] {set {ATTR [Key] = value ;} get {return ATTR [Key] ;}}
- This usage 4: Method of extending objects
/// <Summary> /// abstract description of person /// </Summary> public class person {// <summary> /// gender /// </Summary> Public String sex {set; get ;}} /// <summary> /// auxiliary class /// </Summary> Public static class helper {// <summary> ////******** * ******************** // * This usage 4: extension object Method */////*****************************/ /// </Summary> /// <Param name = "item"> </param> /// <returns> </returns> Public static string getsex (this person item) {return item. sex ;}}
Call:
Person = new person (); person. getsex ();
The complete code is as follows:
Using system; using system. collections. generic; using system. web; using system. collections; using system. collections. specialized; /// <summary> /// abstract description of person /// </Summary> public class person {// <summary> /// name /// </Summary> Public string name {set; get ;}/// <summary >/// gender /// </Summary> Public String sex {set; get ;} /// <summary> /// other attributes /// </Summary> Public namevaluecollection ATTR = new namevaluecollection (); Public Person () {} // <summary> ////**************************** * ************* ///* This usage 1: restrict the member to be hidden by similar names */////************************** * *************** // </Summary> // <Param name = "name"> </param> Public person (string name, string sex) {This. name = Name; this. sex = sex ;} /// <summary> ////****************************** * ************ ///* This usage 2: pass the object as a parameter to another method */////************************** * **************** // </Summary> Public void showname () {helper. printname (this );} /// <summary> ////*************************///// * This usage 3: declare the indexer * // ****************************** // </ summary> /// <Param name = "key"> </param> // <returns> </returns> Public String This [String key] {set {ATTR [Key] = value ;} get {return ATTR [Key] ;}} /// <summary> /// auxiliary class /// </Summary> Public static class helper {// <summary> ////******** * ******************** // * This usage 4: extension object Method */////*****************************/ /// </Summary> /// <Param name = "item"> </param> /// <returns> </returns> Public static string getsex (this person item) {return item. sex ;} /// <summary> /// print the person name /// </Summary> /// <Param name = "person"> </param> Public static void printname (person) {httpcontext. current. response. write ("name:" + person. name + "<br/> ");}}
Call example:
// This usage 1 example person = new person ("", ""); // This usage 2 example person. showname (); // This usage 3 example person ["height"] = "175"; response. write ("height:" + person ["height"] + "<br/>"); person ["weight"] = "110 kg"; response. write ("Weight:" + person ["weight"] + "<br/>"); // This usage 4 example response. write ("Gender:" + person. getsex () + "<br/> ");
Due to the time relationship, we will not talk too much. If there are any shortcomings, please criticize and correct them.
Download the complete sample source code: Http://files.cnblogs.com/foolin/UseThis.rar