C#細節之lambda,linq,匿名方法

來源:互聯網
上載者:User
看代碼,得永生
不再解釋,很容易的,我沒有仔細研究過,在此記下,以後深入研究。using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace Landpy
{
    class Starter
    {
        delegate int Add(int i, int j);
        delegate bool Compare(int i, int j);
        delegate void NoNameMethod(string name);
        static void Main()
        {
            Add add = (x, y) => x + y;
            Console.WriteLine(add.Invoke(1, 2));
            Compare compare = (i, j) => i == j;
            Console.WriteLine(compare.Invoke(1, 2));
            Console.WriteLine(compare.Invoke(1, 1));
            NoNameMethod noNameMethod =
                delegate(string name)
                {
                    Console.WriteLine(String.Format("Hello {0}!", name));
                };
            Action<int> hello = delegate(int i)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.WriteLine("Hello");
                }
            };
            noNameMethod.Invoke("Landpy");
            hello.Invoke(3);

            string[] names = { "++landpy", "pxl", "ljc", "baby" };
            //lambda的寫法 
            var quereynamestwo = names.Where(n => n.Length == 3).Select(n => n.Substring(2));//.Select(n => n.Name);

            //linq的寫法 
            var quereynames = from n in names
                              where n.Length == 3
                              select n;

            foreach (string name in quereynames)
            {
                Console.WriteLine(name);
            }

            foreach (string name in quereynamestwo)
            {
                Console.WriteLine(name);
            }

            ArrayList employees = new ArrayList();

            for (int i = 0; i < 10; i++)
            {
                Employee employee = new Employee();
                employee.Name = "Name" + i.ToString();
                employee.Age = i;
                employees.Add(employee);
            }

            Employee[] employeesArray = (Employee[])employees.ToArray(typeof(Employee));

            var es = employeesArray.Where(n => n.Age > 4);
            foreach (Employee employeeTmp in es)
            {
                Console.WriteLine(employeeTmp.Name);
            }
        }
    }

    class Employee
    {
        public string Name
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.