The jquery approach is concatenating easy to use, simplifying statements and making the code clear and concise. Can the class method of C # also implement similar functions? Based on such doubts, we study the source code of jquery, and find that it is the function method that needs the method concatenating to return the object itself. Since JavaScript is possible, C # should be possible as well.
To verify, write a Jqperson class, and then use the method concatenating to set properties such as its id,name,age, see the following code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceCsharpmethodlikejquery8 {9 Public classJqpersonTen { One stringId {Set;Get; } A stringName {Set;Get; } - intAge {Set;Get; } - stringSex {Set;Get; } the stringInfo {Set;Get; } - - PublicJqperson () - { + - } + /// <summary> A ///sets the ID, which returns this, which is the Jqperson instance at /// </summary> - /// <param name= "Id" ></param> - /// <returns></returns> - PublicJqperson SetId (stringId) - { - This. Id =Id; in return This; - } to /// <summary> + ///returns this, which is the Jqperson instance - /// </summary> the /// <param name= "name" ></param> * /// <returns></returns> $ PublicJqperson SetName (stringname)Panax Notoginseng { - the This. Name =name; + return This; A } the /// <summary> + ///returns this, which is the Jqperson instance - /// </summary> $ /// <param name= "age" ></param> $ /// <returns></returns> - PublicJqperson Setage (intAge ) - { the - This. Age =Age ;Wuyi return This; the } - /// <summary> Wu ///returns this, which is the Jqperson instance - /// </summary> About /// <param name= "Sex" ></param> $ /// <returns></returns> - PublicJqperson Setsex (stringsex) - { - A This. Sex =sex; + return This; the } - /// <summary> $ ///returns this, which is the Jqperson instance the /// </summary> the /// <param name= "info" ></param> the /// <returns></returns> the PublicJqperson SetInfo (stringinfo) - { in the This. Info =info; the return This; About } the /// <summary> the ///ToString Output Key value pair information the /// </summary> + /// <returns></returns> - Public stringtoString () the {Bayi the return string. Format ("Id:{0},name:{1},age:{2},sex:{3},info:{4}", This. Id This. Name, This. Age, This. Sex, This. Info); the - - } the the } the}
You can then test the above to see if the method concatenating is effective:
1 /// <summary>2 ///ToString's Test3 ///</summary>4 [TestMethod ()]5 Public voidtostringtest ()6 {7Jqperson target =NewJqperson ();8Target.setid ("2")9. SetName ("Jack")Ten. Setage ( -) One. Setsex ("Mans") A. SetInfo ("OK"); - stringexpected ="Id:2,name:jack,age:26,sex:man,info:ok"; - stringactual; theactual =target.tostring (); - assert.areequal (expected, actual); - //assert.inconclusive ("Verifies the correctness of this test method. "); -}
As you can see, the method concatenating can actually make the code intuitive and concise, adding readability.
C # Implementation of jquery method concatenating