Small extension big use, oneself expand a foreachread bar

Source: Internet
Author: User

Has the IList extension method been used for Foreach, and depressed IEnumerable does not have this extension? (No use?? Use it, really very convenient, can be less than a few lines!! )

Do I have to use for instead of foreach in order to have an index?

Then this extension method is for you:

 Public Static void Foreachread<t> ( this ienumerable<t> dx,action<int,t> Act) {        int0;       foreach (var in dx)      {          act (i, item);          I+ +;      }}

Finished?? Yes, I'm done.

What's the use of such a thing??

For an operation to use an index, you can only:

list<int> arr =Newlist<int> () {1,2,3,4,5,6,7,8,9,Ten};varQuery=arr. Where (x = x >5). ToList (); for(inti =0; I < query. Count; i++) {Console.WriteLine (string. Format ("{0}:{1}", I,query[i]));} Console.readkey ();

Now you can:

list<intnew list<int1,2,3,4,5, 6,7,8,9,5). Foreachread ((i, O) + = {Console.WriteLine (string. Format ("{0}:{1}", I, O);}); Console.readkey ();

Does it look like jquery!!!?

Very simple things, see others have not sent, come to the bar.

This article comes from Bo-ke-yuan

Small extension big use, oneself expand a foreachread bar

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.