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)