How many times have you used this in C? [Example with source code]

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.