C # concatenate jQuery methods,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.