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 code.
This usage 1: Restrict hidden members with similar names
/// <Summary>
////************************************ ******/
/// * This usage 1: Restrict hidden members with 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>
/// Summary of Person
/// </Summary>
Public class Person
{
/// <Summary>
/// Name
/// </Summary>
Public string Name {set; get ;}
/// <Summary>
////************************************ *******/
/// * This usage 2: passing an 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 person name
/// </Summary>
/// <Param name = "person"> </param>
Public static void PrintName (Person 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>
/// Summary 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: Method of extending the object */
////*****************************/
/// </Summary>
/// <Param name = "item"> </param>
/// <Returns> </returns>
Public static string GetSex (this Person item)
{
Return item. Sex;
}
}
Call:
Person person = new Person ();
Person. GetSex ();
The complete code is as follows:
Show sourceusing System;
Using System. Collections. Generic;
Using System. Web;
Using System. Collections;
Using System. Collections. Specialized;
/// <Summary>
/// Summary 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 hidden members with similar names */
////************************************ ******/
/// </Summary>
/// <Param name = "Name"> </param>
Public Person (string Name, string Sex)
{
This. Name = Name;
This. Sex = Sex;
}
/// <Summary>
////************************************ *******/
/// * This usage 2: passing an 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: Method of extending the object */
////*****************************/
/// </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 person)
{
HttpContext. Current. Response. Write ("Name:" + person. Name + "<br/> ");
}
}
Call example:
Show source // this usage 1 Example
Person person = new Person ("", "");
// This usage 2 example
Person. ShowName ();
// Example 3 of this usage
Person ["Height"] = "175 ";
Response. Write ("Height:" + person ["Height"] + "<br/> ");
Person ["Weight"] = "110 ";
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.
Complete example source code download: http://www.bkjia.com/uploadfile/2011/1013/20111013110305836.rar
Excerpt from: sporadic incidents