码迷,mamicode.com
首页 > 其他好文 > 详细

lambda 复习用

时间:2016-02-24 12:28:13      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

  //筛选
List<Person> persons = PersonsList(); persons = persons.Where(p => p.Age > 6).ToList(); //所有Age>6的Person的集合 persons = persons.Where(p => p.Name.Contains("儿子")).ToList(); //所有Name包含儿子的Person的集合

 

 //委托  逛超市
        delegate int GuangChaoshi(int a);
        static void Main(string[] args)
        {          
           // GuangChaoshi gwl = JieZhang;
            GuangChaoshi gwl = p => p + 10;
            Console.WriteLine(gwl(10) + "");   //打印20,表达式的应用
            Console.ReadKey();
        }      

 

 //委托  逛超市 带参数
        delegate int GuangChaoshi(int a,int b);
        static void Main(string[] args)
        {            
            GuangChaoshi gwl = (p,z) => z-(p + 10);
            Console.WriteLine(gwl(10,100) + "");   //打印80,z对应参数b,p对应参数a
            Console.ReadKey();
        }

 

 //Func<T>委托
static void Main(string[] args)
        {
            Func<int, string> gwl = p => p + 10 + "--返回类型为string";            
            Console.WriteLine(gwl(10) + "");   //打印‘20--返回类型为string’,z对应参数b,p对应参数a
            Console.ReadKey();
        }

 

 //Func<T>委托
static void Main(string[] args)
        {
            Func<int, int, bool> gwl = (p, j) =>
                {
                    if (p + j == 10)
                    {
                        return true;
                    }
                    return false;
                };
            Console.WriteLine(gwl(5,5) + "");   //打印‘True’,z对应参数b,p对应参数a
            Console.ReadKey();
        }

 

//Action委托
public class Test  
{  
    static public Action A;  
    static public Action<int> B;  
    static public Action<int, string> C;  
    static public Action<int, string, int> D;  
    static public Action<int, string, int, string> E;  
  
    static void Main()  
    {  
        A = () =>  
            {  
                Console.WriteLine("I‘m A ");  
            };  
  
        B = (i) =>  
            {  
                Console.WriteLine("I‘m B " + i);  
            };  
  
        C = (i, s) =>  
            {  
                Console.WriteLine("I‘m C " + i + " " + s) ;  
            } ;  
  
        D = (i, s, j) =>  
            {  
                Console.WriteLine("I‘m D " + i + " " + s + " " + j) ;  
            } ;  
  
        E = (i, s, j, t) =>  
            {  
                Console.WriteLine("I‘m E " + i + " " + s + " " + j + " " + t);  
            };  
  
        A();  
        B(1);  
        C(1, "a");  
        D(1, "a", 2);  
        E(1, "a", 2, "b");  
                 
        Console.ReadKey();  

 

lambda 复习用

标签:

原文地址:http://www.cnblogs.com/KQNLL/p/5212435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!