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:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace CSharpMethodLikeJQuery {public class jQPerson {string Id {set; get;} string Name {set; get;} int Age {set; get;} string Sex {set; get;} string Info {set; get;} public jQPerson () {}/// <summary> // set the ID, return this, that is, jQPerson instance // </summary> /// <param name = "Id"> </param> /// <returns> </returns> public jQPerson setId (string id) {this. id = Id; return this;} // <summary> // return this, that is, jQPerson instance // </summary> /// <param name = "name"> </param> /// <returns> </returns> public jQPerson setName (string name) {this. name = name; return this;} // <summary> // return this, that is, jQPerson instance // </summary> /// <param name = "age"> </param> /// <returns> </returns> public jQPerson setAge (int age) {this. age = age; return this;} // <summary> // return this, that is, jQPerson instance // </summary> /// <param name = "sex"> </param> /// <returns> </returns> public jQPerson setSex (string sex) {this. sex = sex; return this;} // <summary> // return this, that is, jQPerson instance // </summary> /// <param name = "info"> </param> /// <returns> </returns> public jQPerson setInfo (string info) {this. info = info; return this ;} /// <summary> /// tostring output key-Value Pair information // </summary> /// <returns> </returns> public string toString () {return string. format ("Id: {0}, Name: {1}, Age: {2}, Sex: {3}, Info: {4}", this. id, this. name, this. age, this. sex, this. info );}}}
Then you can test the above to see if the method concatenation takes effect:
/// <Summary> /// toString test /// </summary> [TestMethod ()] public void toStringTest () {jQPerson target = new jQPerson (); target. setId ("2 "). setName ("jack "). setAge (26 ). setSex ("man "). setInfo ("OK"); string expected = "Id: 2, Name: jack, Age: 26, Sex: man, Info: OK"; string actual; actual = target. toString (); Assert. areEqual (expected, actual); // Assert. inconclusive ("verify the correctness of this test method. ");}
We can see that method concatenation can make the code more intuitive and concise, and increase readability.