C # -- this keyword (2) (including help stickers ),
This time, let's take a look at the second usage of the this keyword:Passing objects as parameters to other methods
----------------------------------------------------------------------------------
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 // assume that you want to convert a student's score to 70%. 8 9 namespace @ this10 {11 class Program12 {13 static void Main (string [] args) 14 {15 Student student = new Student (); 16 student. getMessage ("Mark"); 17 student. printScore (); 18} 19} 20 21 class Student22 {23 public string Name {get; set;} 24 private I Nt Score = 100; 25 26 public void GetMessage (string Name) 27 {28 // this usage belongs to the first type: restrict Member hidden by similar names 29 this. name = Name; 30} 31 32 public int score33 {34 get {return Score;} 35} 36 37 public void PrintScore () 38 {39 Console. writeLine ("My Name Is {0}, My Score Is {1}", Name, score); 40 Console. writeLine ("How many points after converting? "); 41 Console. writeLine ("The Converted Score Is {0}", Convert. convertedScore (this); // Note: The parameter 42} 43} 44 45 class Convert46 {47 public static int ConvertedScore (Student stu) must be passed here) // converts the score to 48 {49 return (int) (0.7 * stu. score); // force type conversion: 50} 51} 52 53}
This in 41 lines of code Convert. ConvertedScore (this) is the "converted score"
To be honest, I don't have a thorough understanding of the usage of this keyword. I am also confused when using this keyword, so I hope that netizens can actively comment on me and give me some help, I am grateful for explaining this point To you.) ---------- To be continued!