C # usage of this pointer,
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.
Usage of this pointer:
1. Restrict hidden members with similar names
The Code is as follows:
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 value of the parameter name is:" + name + "; the value of the parameter num is:" + num); // The output result is "the value of the parameter name is John; parameter num value: 55 "}}
2. Passing objects as parameters to other methods
The Code is as follows:
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 calling a method is: This pointer is passed as a method.
3. Declare the Indexer
private int _Sy; public int this[int sy] { get { return _Sy;} set { sy=_Sy;} }
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.
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C language ^ how to use
A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010
B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011
^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.
//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].