C# 匿名方法 1027

來源:互聯網
上載者:User

標籤:blog   io   ar   for   sp   div   on   art   log   

 class Program {        static void Main(string[] args) {            SorAndShowFiles("Sorted by name", delegate(FileInfo f1, FileInfo 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 DirectoryInfo(@"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);            }        }        //    }

  ///剛開始研究匿名方法和Lambda運算式,試著寫了一個例子

  這段代碼在開始的時候初始化了一個字串列表(string list),然後通過列表的FindAll方法來尋找以“sunny”起始的字串,最後將所尋找到的所有結果輸出。

List<string> names = new List<string>();            names.Add("Sunny Chen");            names.Add("Sunny  Wang");            names.Add("DDDD Crystal");            //第一個階段 匿名方法            List<string> found = names.FindAll(new Predicate<string>(delegate(string name) {                return name.StartsWith("Sunny", StringComparison.OrdinalIgnoreCase);            }));            //方式二 匿名方法            List<string> found2 = names.FindAll(delegate(string name) {                return name.StartsWith("Sunny", StringComparison.OrdinalIgnoreCase);            });            //方式三 Lambda運算式            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# 匿名方法 1027

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.