C # -- this keyword (1 ),
// My C # is followed by Meng Ge (Liu tiemeng) (my formal teacher), and I learned "C # getting started with languages, I also explained some things I don't know. It's a huge fortune and a rare mentor!
When I was learning C #, the teacher used the this keyword in some examples. Meng gave a slight explanation in the video and did not explain it in depth, I still have questions on this part: how can I use this keyword?
So I looked for some information this afternoon and did not know whether my understanding was correct. I hope you can give me some guidance.
Starting fromMicrosoft's official C # programming guideExcerpt section:
The following are common uses of this:
---------------------------------------------------------
For the first purposeRestrict hidden members with similar namesI have just got a clue that my language organization capability is poor. Let's take a look at it through an example:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace @this{ class Program { static void Main(string[] args) { Student stu = new Student(); stu.GetMessage("Mark", 1); Console.WriteLine("My name is {0}.My ID number is {1}.",stu.Name,stu.ID); } } class Student { public string Name { get; set; } public int ID { get; set; } public void GetMessage(string Name,int ID) { this.Name = Name; this.ID = ID; } }}
This. Name = Name; // The Name here is the Name passed in.
This. ID = ID; // The ID here is the uploaded ID.
This refers to an instance created in the Student class (or replaced by this)
Let's change the code:
public void GetMessage(string Name,int ID) { Student stu = new Student(); stu.Name = Name; stu.ID = ID; }
We created an instance named stu in this class and assigned a value for "himself" through it. After running, the program is exactly the same.
Bytes -----------------------------------------------------------------------------------------
Make a summary of the first purpose:
To put it bluntly, we can think that this creates an instance for us and then uses this instance for a series of operations.
Bytes ------------------------------------------------------------------------------------------
To be Continued!
Bytes ------------------------------------------------------------------------------------------
I hope that the majority of users will point out the problem, point out where I have understood the mistake, communicate with each other, and make progress together!