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