C # anonymous method 1027

Source: Internet
Author: User

Class Program {        static void Main (string[] args) {            sorandshowfiles (' Sorted by name ', delegate (FileInfo F1, Fileinf o F2) {                return F1.Name.CompareTo (F2. Name);            }                );            Console.readkey ();        }        delegate void Sampledelegate (string x);        public static void Candidateaction (String x) {            Console.WriteLine ("Candidateaction");        }        public static void Sorandshowfiles (string title, Comparison<fileinfo> sortoder) {            fileinfo[] files = new Direc Toryinfo (@ "F:\"). GetFiles ();            Array.Sort (Files, sortoder);            Console.WriteLine (title);            foreach (FileInfo file in files) {                Console.WriteLine ("Name:{0}   length:{1} bytes", File. Name, file. Length);            }        }        //    }

Just started studying anonymous methods and lambda expressions, trying to write an example

   This code initializes a string list at the beginning, and then uses the FindAll method of the list to find the string starting with "sunny" and finally outputs all the results found.

list<string> names = new list<string> ();            Names. ADD ("Sunny Chen");            Names. ADD ("Sunny  Wang");            Names. ADD ("DDDD Crystal");            The first phase of the anonymous method            list<string> found = names. FindAll (New Predicate<string> (delegate (string name) {                return name. StartsWith ("Sunny", stringcomparison.ordinalignorecase);            });            Way two anonymous methods            list<string> found2 = names. FindAll (Delegate (string name) {                return name. StartsWith ("Sunny", stringcomparison.ordinalignorecase);            });            The three-way lambda expression            list<string> found3=names. FindAll (x=> (X.startswith ("Sunny", StringComparison.OrdinalIgnoreCase));            if (found!=null) {                foreach (string  item in found) {                    Console.WriteLine (item);                }            }            Console.readkey ();

  

C # anonymous method 1027

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.