zoukankan      html  css  js  c++  java
  • Lambda表达式

    1. lambda表达式是一个匿名函数,匿名方法。

    namespace lambda
    {
        public delegate void NoReturnWithPara(string name); //声明委托
        class LambdaShow
        {
            public void Show()
            {
    
                {
                    NoReturnWithPara method = new NoReturnWithPara(this.Study);   //声明委托实例,调用方法
                    method.Invoke("YOYO");
                }
    
                {
                    NoReturnWithPara method = new NoReturnWithPara(     //单独声明的方法换成匿名方法
                        delegate (string name)
                        {
                            Console.WriteLine("{0} is studying lambda !", name);
                        }
                     );
                }
    
                {
                    NoReturnWithPara method = new NoReturnWithPara(     //单独声明的方法换成匿名方法
                      (string name) =>//delegate关键字换成了goes to =>
                      {
                            Console.WriteLine("{0} is studying lambda !", name);
                      }
                     );
                }
    
                {
                    NoReturnWithPara method = new NoReturnWithPara(     //单独声明的方法换成匿名方法
                      (name) =>//委托实例化要求方法的签名必须和委托一致
                      {
                          Console.WriteLine("{0} is studying lambda !", name);
                      }
                     );
                }
    
                {
                    NoReturnWithPara method = new NoReturnWithPara(     //单独声明的方法换成匿名方法
                      name=>//只有一个参数可以去掉小括号;如果方法体只有一行,大括号和分号也都可以去掉  
                          Console.WriteLine("{0} is studying lambda !", name)
                     );
                }
    
                {
                    //委托实例化可以不要new
                    NoReturnWithPara method = name => Console.WriteLine("{0} is studying lambda !", name);
                 }
              
            }
            private void Study(string name)
            {
                Console.WriteLine("{0} is studying lambda !", name);
            }
        }
    }

    2. .net提供了两个标准的委托  Action 和 Func

    //.net提供了两个标准的委托
    
                // public delegate void Action(); 无参数无返回值
                Action act1 = () => Console.WriteLine("test");
    
                //public delegate void Action<in T>(T obj); 有参数无返回值的泛型委托
                Action<string> act2 = name => Console.WriteLine("test");
                Action <int,string > act4=(id,name) => Console.WriteLine("test");
    
                //0个泛型参数,无返回值的委托
                Action<string, int, double, DateTime, long, decimal, float, string, int, double, DateTime, long, decimal, float, decimal, float> act3 = null;
    
                // public delegate TResult Func<out TResult>(); 无参数有一个返回值的委托
                //里面的in,out代表输入参数,输出结果
                //这里不需要return,直接给返回值
                Func<int> fun1 = () => 3;
    
                // 一个参数一个返回值的委托
                Func<string, int> fun2 = s => 3;
    
                //两个参数一个返回值的委托
                Func<string, int, int> fun3 = (s, i) => 3;
    
                //16个参数一个返回值的委托
                Func<string , string, int, double, DateTime, long, decimal, float, string, int, double, DateTime, long, decimal, float, decimal, float> fun4 = null;
                

    3. linq扩展展示

        class LinqShow
        {
            public static void Show()
            {
                List<int> intList = new List<int>()
               { 21,56,95,234,854,12,456};
    
                //Func<int, bool> func = t => t > 100;
                foreach (var item in intList .Where(t=>t>100)) //陈述式的语法
                {
                    Console.WriteLine(item);
                }
    
                foreach (var item in intList.Where(t => t > 100)
                    .Select(t=>string .Format ("这里是数字{0}",t))
                    .OrderBy(t=>t)) 
                {
                    Console.WriteLine(item);
                }
            }
        }

        

  • 相关阅读:
    微软解决方案框架MSF4.0的预发布版本支持敏捷过程了
    Jackson 框架,轻易转换JSON
    [ lucene扩展 ] Spelling Checker using Lucene
    [ solr扩展 ] Solr Spellchecker internals (now with tests!)
    [ lucene扩展 ] An Introduction to Apache Lucene for FullText Search
    linux中 jdk 的卸载和安装[转]
    hadoop入门之单机版环境搭建[转]
    Redhat上两台服务器建立SSH信任关系[转]
    MYSQL常用命令[转]
    linux下vi命令大全
  • 原文地址:https://www.cnblogs.com/xiao9426926/p/6085601.html
Copyright © 2011-2022 走看看