C # concatenate jQuery methods,
JQuery method concatenation is very convenient to use, and can simplify the statements to make the code clear and concise. Can C # class methods implement similar functions? Based on these questions, I studied jQuery's source code and found that the function method that requires method concatenation finally returns the object itself. Since javascript can be used, C # should also be supported.
For verification, write a jQPerson class and set its attributes such as ID, Name, and Age with method concatenation. See the following code:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace CSharpMethodLikeJQuery 8 {9 public class jQPerson10 {11 string Id {set; get;} 12 string Name {set; get;} 13 int Age {set; get ;} 14 string Sex {set; get;} 15 string Info {set; get;} 16 17 public jQPerson () 18 {19 20} 21 // <summary> 22 // set the ID, return this, that is, jQPerson instance 23 // </summary> 24 /// <param name = "Id"> </param> 25 /// <returns> </returns> 26 public jQPerson setId (string Id) 27 {28 this. id = Id; 29 return this; 30} 31 // <summary> 32 // return this, that is, jQPerson instance 33 // </summary> 34 /// <param name = "name"> </param> 35 /// <returns> </returns> 36 public jQPerson setName (string name) 37 {38 39 this. name = name; 40 return this; 41} 42 // <summary> 43 // return this, that is, jQPerson instance 44 /// </summary> 45 /// <param name = "age"> </param> 46 /// <returns> </returns> 47 public jQPerson setAge (int age) 48 {49 50 this. age = age; 51 return this; 52} 53 // <summary> 54 // return this, that is, jQPerson instance 55 // </summary> 56 /// <param name = "sex"> </param> 57 /// <returns> </returns> 58 public jQPerson setSex (string sex) 59 {60 61 this. sex = sex; 62 return this; 63} 64 // <summary> 65 // return this, that is, jQPerson instance 66 // </summary> 67 /// <param name = "info"> </param> 68 /// <returns> </returns> 69 public jQPerson setInfo (string info) 70 {71 72 this. info = info; 73 return this; 74} 75 // <summary> 76 // tostring output key-Value Pair information 77 /// </summary> 78 /// <returns> </returns> 79 public string toString () 80 {81 82 return string. format ("Id: {0}, Name: {1}, Age: {2}, Sex: {3}, Info: {4}", this. id, this. name, this. age, this. sex, this. info); 83 84 85} 86 87} 88}
Then you can test the above to see if the method concatenation takes effect:
1 /// <summary> 2 /// toString Test 3 /// </summary> 4 [TestMethod ()] 5 public void toStringTest () 6 {7 jQPerson target = new jQPerson (); 8 target. setId ("2") 9. setName ("jack") 10. setAge (26) 11. setSex ("man") 12. setInfo ("OK"); 13 string expected = "Id: 2, Name: jack, Age: 26, Sex: man, Info: OK"; 14 string actual; 15 actual = target. toString (); 16 Assert. areEqual (expected, actual); 17 // Assert. inconclusive ("verify this test method . "); 18}
We can see that method concatenation can make the code more intuitive and concise, and increase readability.