C # Several uses of this pointer (this is plagiarism)

Source: Internet
Author: User

Several uses of the C # this pointer

1. Limit the members that are hidden by similar names

C # codeCopy
Public ClassThisname{Public StringName= "Tom";Public IntNum= 55;PublicThisname (){ } Public voidGetthisname (StringNameIntNum{Name= This. Name;//Tune global variable nameNum= this. Num;  adjust the global variable num HttpContext.Current.Response.Write ("The value of the parameter name is:"+name+"; The value of the parameter num is:"+num";  The output is "parameter name value is Zhang San; the parameter num value is:" } }          

2. Passing objects as parameters to other methods

C # codeCopy
Public  ClassThisff{PublicTHISFF (){ } Public StringShuju (){Return "This pointer is passed as a method"; } Public voidF1 (THISFF ff){HttpContext.Current.Response.Write (Ff.shuju ());} Public void f () {F1 (this);  This can be understood here as THISFF Ff=news THISFF (); Instance of the current class } } thisff ff=news THISFF ();  Method Example ff.f ();  the calling method outputs the result: the this pointer is  passed as a method            

3. Declaring indexers

C # codeCopy
Private Int_sy;Public int this [int sy] { get { return _sy;}  Set {sy=_sy;}  }< /c1>

Ii. Summary of this in C #

1. The This keyword refers to the current instance where the member is accessed. The static member function does not have the this pointer. The This keyword can be used to access members from constructors, instance methods, and instantiation accessors. cannot be in a static method. The This keyword is used in the static property accessor or in the variable initializer of the domain declaration, which results in an error.

2. This as a value type in the constructor of the class represents a reference to the object being constructed.

3. Occurs in a method of a class this is a value type that represents a reference to the object that called the method.

4. This as a variable type represents a reference to the structure being constructed, which appears in the structure's constructor.

5, the structure of the method appears in the this as a variable type represents the structure of the call to the method.

C # Several uses of this pointer (this is plagiarism)

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.