C#LAMBDA前pressions:我为什么要使用它们? [英] C# Lambda expressions: Why should I use them?

查看:182
本文介绍了C#LAMBDA前pressions:我为什么要使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也很快在微软LAMBDA前pression 的文档阅读。

I have quickly read over the Microsoft Lambda Expression documentation.

这样的例子帮助我更好地理解,虽然:

This kind of example has helped me to understand better, though:

delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25

不过,我不明白为什么它是这样一个创新。这只是当方法变量结束了死亡的方法,对吗?为什么我要使用它来代替真正的方法是什么?

Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method?

推荐答案

<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-ex$p$pssions.aspx\">Lambda前pressions 是匿名委托一个简单的语法,到处可以使用匿名委托可以使用。然而,相反的是不正确的;拉姆达前pressions可以转换为前pression树,使得很多喜欢的LINQ to SQL的魔力。

Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be used. However, the opposite is not true; lambda expressions can be converted to expression trees which allows for a lot of the magic like LINQ to SQL.

以下是 LINQ到对象的实例前pression使用匿名与会代表则拉姆达前pressions展示如何更容易对眼睛,他们是:

The following is an example of a LINQ to Objects expression using anonymous delegates then lambda expressions to show how much easier on the eye they are:

// anonymous delegate
var evens = Enumerable
                .Range(1, 100)
                .Where(delegate(int x) { return (x % 2) == 0; })
                .ToList();

// lambda expression
var evens = Enumerable
                .Range(1, 100)
                .Where(x => (x % 2) == 0)
                .ToList();

LAMBDA前pressions和匿名委托有优势写一个单独的功能:它们实现关闭它可以让你通过本地状态的功能,无需添加参数以功能或制作一个 - 时间使用的对象。

Lambda expressions and anonymous delegates have an advantage over writing a separate function: they implement closures which can allow you to pass local state to the function without adding parameters to the function or creating one-time-use objects.

防爆pression树木是一个非常的C#3.0强大的新功能,它允许一个API,看一个前pression而不是刚开到可以执行的方法的引用的结构。一个API只是有作出委托参数到防爆pression&LT; T&GT; 参数,编译器会生成一个lambda一个前pression树,而不是匿名委托:

Expression trees are a very powerful new feature of C# 3.0 that allow an API to look at the structure of an expression instead of just getting a reference to a method that can be executed. An API just has to make a delegate parameter into an Expression<T> parameter and the compiler will generate an expression tree from a lambda instead of an anonymous delegate:

void Example(Predicate<int> aDelegate);

称为这样的:

Example(x => x > 5);

变成了:

void Example(Expression<Predicate<int>> expressionTree);

后者将获得通过的抽象语法树的重新presentation描述前pression X'GT; 5 。 LINQ to SQL的依赖于这种行为能够把C#的前pressions到SQL前pressions所需的服务器端过滤/排序/等。

The latter will get passed a representation of the abstract syntax tree that describes the expression x > 5. LINQ to SQL relies on this behavior to be able to turn C# expressions in to the SQL expressions desired for filtering / ordering / etc. on the server side.

这篇关于C#LAMBDA前pressions:我为什么要使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆