This keyword usage in methods in ASP. net mvc (this htmlhelper helper)

Source: Internet
Author: User

ASP. net MVC2 programming model, found that there is a very strange use of this, has never been seen before, after two hours of reading the information to find finally come to the conclusion, I 'd like to share with you (I hope you can correct me if I have an incomplete understanding)

Public static string scheduleentries (this htmlhelper helper ,...)

The question I found on a foreign forum is:

The way it works for MVC is thatHtmlIn the view is a property of htmlhelper type on the viewpage class.

I have probably understood it, which means "using HTML in the view as an htmlhelper type attribute". Here, the HTML is not quite clear, I guess it only refers to the current object, that is, taking the current object as a parameter of htmlhelper. I can only understand this sentence.

Later I saw a paging control written by MVC, which also has the same syntax:

 
Public static ipagedlist <t> topagedlist <t> (this ienumerable <t> source, int pageindex, int pagesize, int totalcount) {return New pagedlist <t> (source, pageindex, pagesize, totalcount );}

Then I checked how to call this method and found that this method can be called as long as it inherits the ienumerable <t> interface, and the first parameter is hidden, this makes it clearer. Here, the topagedlist <t> (...) The method is added to the class of the inherited interface ienumerable <t>. The parameter with this in this method is automatically the current object with this method by default, by understanding this, We can boldly use the methods.

 
Private ilist <product> allproducts = new list <product> (); ipagedlist <product> productlist this. allproducts. topagedlist (currentpageindex, defaultpagesize)

The above two rowsCodeIt is an example of how to call a method with the this keyword, hoping to help you and help you understand it. If something is wrong, you are welcome to correct it.

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.