C# 什么是设备以及如何使用 lambda 表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-02 10:29:27  来源:igfitidea点击:

What is appliance and how to use lambda expressions?

提问by Danimal

I've read that Lambda Expressions are an incredibly powerful addition to C#, yet I find myself mystified by them. How can they improve my life or make my code better? Can anyone point to a good resource for learning such expressions?

我读到 Lambda 表达式是 C# 的一个非常强大的补充,但我发现自己对它们感到困惑。他们如何改善我的生活或使我的代码更好?任何人都可以指出学习此类表达的好资源吗?

They seem cool as hell, but how do they relate to my day-to-day life as an asp.net developer?

它们看起来很酷,但它们与我作为 asp.net 开发人员的日常生活有什么关系?

Edit: Thanks for the examples, and thanks for the link to Eric White's articles. I'm still digesting those now. One quick question: are lambda expressions useful for anything other than querying? Every example I've seen has been a query construct.

编辑:感谢您提供示例,并感谢您提供 Eric White 文章的链接。我现在还在消化这些。一个简单的问题:lambda 表达式除了查询之外还有其他用途吗?我见过的每个例子都是一个查询结构。

采纳答案by Will Dean

: are lambda expressions useful for anything other than querying

: lambda 表达式对查询以外的任何事情都有用吗

Lamba expressions are nothing much other than a convenient way of writing a function 'in-line'.

Lamba 表达式只不过是一种“内联”编写函数的便捷方式。

So they're useful any place you wanted a bit of code which can be called as though it's a separate function but which is actually written inside its caller. (In addition to keeping related code in the same location in a file, this also allows you to play fun games with variable scoping - see 'closures' for a reference.)

所以它们在任何你想要一些代码的地方都很有用,这些代码可以被调用,好像它是一个单独的函数,但实际上是在它的调用者内部编写的。(除了将相关代码保存在文件中的同一位置之外,这还允许您玩具有可变作用域的有趣游戏 - 请参阅“闭包”以获取参考。)

An example of a non-query-related use of a lamba might be a bit of code which does something asynchronously that you start with ThreadPool.QueueUserWorkItem. The important point is that you could also write this using anonymous delegates (which were a C#2 introduction), or just a plain separate class member function.

Lamba 的非查询相关使用的一个示例可能是一些代码,它以 ThreadPool.QueueUserWorkItem 开始异步执行某些操作。重要的一点是,您也可以使用匿名委托(这是 C#2 介绍)或仅使用简单的单独类成员函数来编写它。

This http://blogs.msdn.com/jomo_fisher/archive/2005/09/13/464884.aspxis a superb step-by-step introduction into all this stuff, which might help you.

这个http://blogs.msdn.com/jomo_fisher/archive/2005/09/13/464884.aspx是对所有这些东西的极好的逐步介绍,它可能对您有所帮助。

回答by Brian Leahy

Lambdas bring functional programing to C#. They are anonymous functions that can be passed as values to certain other functions. Used most in LINQ.

Lambda 将函数式编程引入 C#。它们是匿名函数,可以作为值传递给某些其他函数。在 LINQ 中使用最多。

Here is a contrived example:

这是一个人为的例子:

List<int> myInts = GetAll();
IEnumerable<int> evenNumbers = myInts.Where(x => x % 2 == 0);

Now when you foreach through evenNumbers the lamda

现在当你通过 evenNumbers foreach lamda

x=> x % 2 == 0

is then applied as a filter to myInts.

然后作为过滤器应用于 myInts。

They become really useful in increasing readability to complicated algorithms that would have many nested IF conditionals and loops.

它们在提高具有许多嵌套 IF 条件和循环的复杂算法的可读性方面变得非常有用。

回答by Corey

Here's a simple example of something cool you can do with lambdas:

这是一个简单的例子,你可以用 lambdas 做一些很酷的事情:

List<int> myList = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
myList.RemoveAll(x => x > 5);
//myList now == {1,2,3,4,5}

The RemoveAll method takes a predicate(a delegate that takes argurments and returns a bool), any that match it get removed. Using a lambda expression makes it simpler than actually declaring the predicate.

RemoveAll 方法接受一个谓词(一个接受参数并返回一个 bool 的委托),任何匹配它的都被删除。使用 lambda 表达式比实际声明谓词更简单。