Part 99 Lambda expression in c#

class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>() {
                new Person{ID=101,Name="lin1"},
                new Person{ID=102,Name="lin2"},
                new Person{ID=103,Name="lin3"}
            };

            Person person = persons.Find(
                delegate(Person p)          //this is an anonymous method.
                {
                    return p.ID == 101;
                }
                );
            Person p1 = persons.Find(p=>p.ID==101);//using lambda expression
            Person p2 = persons.Find((Person p)=>p.ID==101);//you can also explicitly the input type but no required
            Console.WriteLine("person id={0},name={1}", person.ID, person.Name);

        }
    }
    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

=> is called lambda operator and read as Goes To. Notice that with a lambda expression you don‘t have to use the delegate keyword explicitly and don‘t have to specify the input parameter type explicity. The parameter type is inferred(推倒出来). lambda expressions are more convenient to use than anonymous methods. lambda expressions are particularly helpful for writing LINQ query expressions.

In most of the cases lambda expressions supersedes(替代) anonymous methods. To my knowlege, the only time I prefer to use anonymous methods over lambdas is, when we have to omit(省略) the parameter list when it‘s not used within the body.

Anonymous methods allow the parameter list to be omitted entirely when it‘s not used within the body,where as with lambda expressions this is not the case.

For example, with anonymous method notice that we have omitted the parameter list as we are not using them within the body

Button.Click += delegate{MessageBox.Show("hello world.");};

The above code can be rewritten using lambda expression as shown below.Notice that with lambda we cannot omit the parameter list.

Button.Click+=(sender,e)=>{MessegeBox.Show("hello world.");};
Button.Click+=()=>{MessegeBox.Show("hello world.");};//if omit parameter list it will get a compilar error.
时间: 2024-10-05 06:16:39

Part 99 Lambda expression in c#的相关文章

Delegates, Events and Lambda Expression

The content and code of this article is referenced from book Pro C#5.0 and the .NET 4.5 Framework by Apress. The intention of the writing is to review the konwledge and gain better understanding of the .net framework.    Up to this point, most of the

Android 使用Java8新特性之Lambda expression

前言 Lambda expression,java8的新特性.使用Lambda expression,可以替代只有一个函数的接口实现,告别匿名内部类,代码看起来更简洁易懂. java8还有其它一些新特性,不过在android上可能都无法使用. studio 2.x后 支持jack编译器,使用它,能使用java8的Lambda expression,但其它特性也不敢保证就能用. 注:Android SDK中集成了JDK的一些源码,有些原生JDK中的类,可能增加了新特性的一些实现,但Android中

java8函数表达式的定义[Definition of a Lambda Expression]

英文来源于:Java in a Nutshell, 6th Edition Definition of a Lambda Expression A lambda expression is essentially a function that does not have a name, and can be treated as a value in the language. As Java does not allow code to run around on its own outsi

Lambda Expression in C#

1.Expression Expression<Func<double, double>> exp = a => Math.Sin(a); 委托类型Func<double, double>,它限定生成的表达式树是一个接受double,并返回double的一元Lambda函数 <Func<double, double, double, double, double> 输入参数为4个double,返回一个double类型 static void Ma

JDK 1.8 Lambda Expression 的优点与限制

我们知道JDK 1.8新增了Lambda Expression这一特性. 那么,JDK 1.8为什么要新增这个特性呢? 这个特性给JDK 1.8带来了什么好处? 它在JDK 1.8中可以做什么?不可以做什么? 在这篇文章,我打算简单聊聊这些话题. 1. Lambda Expression是什么? Lambda Expression,又名 Anonymous function,  它起源于Alonzo Church在1936年提出的 lambda calculus. 这是数理逻辑中的一个概念,具体

C++ lambda expression

Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing variables in scope. 1. syntax of a lambda expression [ captures ] <tparams>(optional)(c++20) ( params ) specifiers exception attr -> ret requires(o

java Lambda expression

Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). 使用 Lambda 表达式可以使代码变的更加简洁紧凑. 语法 lambda 表达式的语法格式如下: (parameters) -> expression 或 (parameters) ->{ statements; } 以下是lambda表达式的重要特征: 可选类型声明:不需要声明参数类型,编译器可以统一识别参数值. 可选的参数圆括号:一个

函数式编程之根-λ表达式(lambda expression)

学习函数式编程的大图(big map)/鸟瞰图,并在没有掌握Scheme的各种语言细节之前,给出Scheme代码.这意味着我们不需要看懂源代码,而是将这里的介绍作为后续学习的大图,使自己知道身在何处: 1930s初,普林斯顿大学的逻辑学家阿伦佐·丘奇 (Alonzo Church,1903-1995) 开发出了一种新的形式系统(formal system),即拉姆达运算/演算 (λ-calculus .lambda calculus ,lambda即希腊字母λ). λ运算的核心是λ表达式,以此形

C++11 lambda表达式(lambda expression)

1.可调用对象(callable object)类别包括: 函数 函数指针 重载了函数调用运算符的类 lambda 表达式 2.lambda表达式形式: [capture list] (parameter list) -> return type { function body } capture list (捕获列表)是一个 lambda 所在函数中定义的局部变量的列表(通常为空),空捕获列表表明此 lambda 不使用它所在的函数中的任何局部变量, return type.parameter