我已经快速阅读了Microsoft Lambda表达式文档。
不过,这样的例子帮助我更好地理解:
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
不过,我还是不明白为什么这是一种创新。它只是一个在“方法变量”结束时死亡的方法,对吧?为什么我要用这个而不是真正的方法?
我已经快速阅读了Microsoft Lambda表达式文档。
不过,这样的例子帮助我更好地理解:
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
不过,我还是不明白为什么这是一种创新。它只是一个在“方法变量”结束时死亡的方法,对吧?为什么我要用这个而不是真正的方法?
当前回答
Lambda表达式是表示匿名方法的一种简洁方式。匿名方法和Lambda表达式都允许您内联定义方法实现,但是,匿名方法显式地要求您定义方法的参数类型和返回类型。Lambda表达式使用c# 3.0的类型推断特性,该特性允许编译器根据上下文推断变量的类型。这是非常方便的,因为这节省了我们大量的输入!
其他回答
例如,Lambda表达式使任务简单得多
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var oddNumbers = numbers.Where(x => x % 2 != 0);
var sumOfEven = numbers.Where(x => x % 2 == 0).Sum();
在上面的代码中,因为我们使用了lambda,所以我们在一行代码中得到奇数和偶数的和。
如果没有lambda,我们将不得不使用if/else或for循环。
因此,使用lambda来简化c#中的代码是很好的。
一些关于它的文章:
https://qawithexperts.com/article/c-sharp/lambda-expression-in-c-with-examples/470
https://exceptionnotfound.net/csharp-in-simple-terms-18-expressions-lambdas-and-delegates
http://dontcodetired.com/blog/post/Whats-New-in-C-10-Easier-Lambda-Expressions
Lambda表达式是表示匿名方法的一种简洁方式。匿名方法和Lambda表达式都允许您内联定义方法实现,但是,匿名方法显式地要求您定义方法的参数类型和返回类型。Lambda表达式使用c# 3.0的类型推断特性,该特性允许编译器根据上下文推断变量的类型。这是非常方便的,因为这节省了我们大量的输入!
这是一种将小操作放在非常接近使用位置的地方的方法(与在接近使用点的地方声明变量没有什么不同)。这将使您的代码更具可读性。通过匿名化表达式,如果在其他地方使用了该函数并对其进行了修改以“增强”它,那么其他人就很难破坏您的客户端代码。
同样,为什么需要使用foreach?你可以用一个简单的for循环来完成foreach中的所有事情,或者直接使用IEnumerable。答案:你不需要它,但它使你的代码更可读。
Microsoft has given us a cleaner, more convenient way of creating anonymous delegates called Lambda expressions. However, there is not a lot of attention being paid to the expressions portion of this statement. Microsoft released a entire namespace, System.Linq.Expressions, which contains classes to create expression trees based on lambda expressions. Expression trees are made up of objects that represent logic. For example, x = y + z is an expression that might be part of an expression tree in .Net. Consider the following (simple) example:
using System;
using System.Linq;
using System.Linq.Expressions;
namespace ExpressionTreeThingy
{
class Program
{
static void Main(string[] args)
{
Expression<Func<int, int>> expr = (x) => x + 1; //this is not a delegate, but an object
var del = expr.Compile(); //compiles the object to a CLR delegate, at runtime
Console.WriteLine(del(5)); //we are just invoking a delegate at this point
Console.ReadKey();
}
}
}
This example is trivial. And I am sure you are thinking, "This is useless as I could have directly created the delegate instead of creating an expression and compiling it at runtime". And you would be right. But this provides the foundation for expression trees. There are a number of expressions available in the Expressions namespaces, and you can build your own. I think you can see that this might be useful when you don't know exactly what the algorithm should be at design or compile time. I saw an example somewhere for using this to write a scientific calculator. You could also use it for Bayesian systems, or for genetic programming (AI). A few times in my career I have had to write Excel-like functionality that allowed users to enter simple expressions (addition, subtrations, etc) to operate on available data. In pre-.Net 3.5 I have had to resort to some scripting language external to C#, or had to use the code-emitting functionality in reflection to create .Net code on the fly. Now I would use expression trees.
这只是使用lambda表达式的一种方式。你可以在任何可以使用委托的地方使用lambda表达式。这允许你做这样的事情:
List<string> strings = new List<string>();
strings.Add("Good");
strings.Add("Morning")
strings.Add("Starshine");
strings.Add("The");
strings.Add("Earth");
strings.Add("says");
strings.Add("hello");
strings.Find(s => s == "hello");
这段代码将在列表中搜索与单词“hello”匹配的条目。另一种方法是传递一个委托给Find方法,像这样:
List<string> strings = new List<string>();
strings.Add("Good");
strings.Add("Morning")
strings.Add("Starshine");
strings.Add("The");
strings.Add("Earth");
strings.Add("says");
strings.Add("hello");
private static bool FindHello(String s)
{
return s == "hello";
}
strings.Find(FindHello);
编辑:
在c# 2.0中,这可以使用匿名委托语法完成:
strings.Find(delegate(String s) { return s == "hello"; });
Lambda明显地清理了语法。