19.c# describes the extension methods in IEnumerable and Ienumerable<t> (10.3-10.5)

Source: Internet
Author: User

Today, there is not much speech, only code, extension methods too much, can not be listed, some books, see more will be used.

1 //Enumerable.range Returns the start to end range, which is a enumrable<int> type2 //The range method does not really construct a list with the appropriate numbers, it only generates those numbers at the right time, "just in"3 varC0 = Enumerable.range (1,Ten);4 foreach(varEinchC0)5 {6Console.WriteLine (e);//Print 1,2,3,4,5,6,7,8,9,107 }8 9 //Reverse (), reverse listTen varC1 =C0. Reverse (); One foreach(varEinchC1) A { -Console.WriteLine (e);//Print 10,9,8,7,6,5,4,3,2,1 - } the  - //where (), filtering - varC2 = C1. Where (x = x%2==0);//returns the element that conforms to the predicate condition, the return type is enumrable<int> - foreach(varEinchC2) + { -Console.WriteLine (e);//10,8,6,4,2 + } A  atConsole.WriteLine ("---------------------------------------------"); -  - //use chained operations, because each operation returns enumrable<int> -C0. Reverse (). Where (x = x%2==0). ToList (). ForEach (x =Console.WriteLine (x)); -  - //Select (), drop shadow, return to List of custom type objects in string[] Arrs = {"James","John","Michelle","Amy","Kim" }; -  to //Returns a list of anonymous types that contain the first and last attributes + varOBJS = Arrs. Select (x =New{first = X.first (), last =x.last ()}); - foreach(varEinchObjs) the { *Console.WriteLine (E.first);//j,j,m,a,k $Console.WriteLine (E.last);//s,n,e,y,mPanax Notoginseng } -  the //Select the element that starts with "J" and move y from the first to the non- + varYOBJS = Arrs. ToList (). FindAll (x = X.startswith ("J")). Select (x = A { the     vary = X.remove (0,1); +     returnY +"J"; - }); $ foreach(varEinchYobjs) $ { -Console.WriteLine (e);//Amesj,ohnj - } the  - //sort using order BY, return iorderedenumerable<string>Wuyi varOrderarrs = Arrs. (x =X.first ()); the foreach(varEinchOrderarrs) - { WuConsole.WriteLine (e);//Amy,james,john,kim,michelle - } About  $ //ThenBy (), first sorted by First order (), and then in unsorted order - varThenarrs = Arrs. (x = X.first ()). ThenBy (x =x.last ()); - foreach(varEinchThenarrs) - { AConsole.WriteLine (e);//Amy,john,james,kim,michelle +}

Please treatise.

19.c# describes the extension methods in IEnumerable and Ienumerable<t> (10.3-10.5)

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.