Example of this pointer usage in C,

Source: Internet
Author: User

Example of this pointer usage in C,

This article mainly introduces the usage of this pointer in C #. It is a very important concept for beginners and must be mastered. If you need it, you can refer to it.

This example demonstrates the usage of the this pointer in C #, which is helpful for beginners to further master C #. The specific content is as follows:

I. What is the this pointer:

Here are some object-oriented programming concepts that need to be explained: The concept of Class and the concept of Object.
Class is a generalization of things, and it is also the basic unit of ownership of all code in C # encoding. The object is an instantiation of the class, that is, the return value of the new method in C. Writing code cannot directly use the operation class, but can only instantiate the class first, and then we use the object after the class is instantiated.
Generally, a "class" is like a "person". After instantiating a class, it becomes an "object" for a single person, for example, "Zhang San ".
This is the "object" after the instantiation of the "class" that your code belongs to, also called the current object.

Ii. usage of this pointer:

1. Restrict hidden members with similar names

The main code is as follows:

123456789101112 public class ThisName { public string name = "James"; public int num = 55; public ThisName() { } public void GetThisName(string name, int num) { name = this.name;// Retrieve the global variable name num = this.num;// Retrieve the global variable num HttpContext.Current.Response.Write("The parameter name value is :"+name+"; The value of the num parameter is :"+num);// The output result is "parameter name value is Zhang San; parameter num value: 55" } }

2. Passing objects as parameters to other methods

The main code is as follows:

1234567891011121314151617181920212223 public class ThisFF { public ThisFF() { } public string shuju() { return "This pointer is passed as a method"; }   public void f1(ThisFF ff) { HttpContext.Current.Response.Write(ff.shuju()); }   public void f() { f1(this);// This can be understood as ThisFF ff = news ThisFF (); instance of the current class } }   ThisFF ff=news ThisFF();// Method Instance ff.f();// The output result of the call method is: This pointer is passed as a method

3. Declare the Indexer

The main code is as follows:

123456 private int _Sy; public int this[int sy] { get { return _Sy;} set { sy=_Sy;} }

Iii. Summary:

1. this keyword references the current instance of the accessed member. The static member function does not have the this pointer. This keyword can be used to access members from constructors, instance methods, and instantiated accessors.
The method cannot be static. This keyword is used in the static property accessors or the variable initialization program declared by the domain. this produces an error.

2. this appears in the constructor of the class as a value type to indicate reference to the object itself being constructed.

3. In the class method, this is used as a value type to indicate reference to the object that calls this method.

4. this appears in the constructor of the structure as a variable type to indicate reference to the structure being constructed.

5. this appears in the structure method as a variable type to indicate the structure for calling this method.

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.