我已经快速阅读了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
不过,我还是不明白为什么这是一种创新。它只是一个在“方法变量”结束时死亡的方法,对吧?为什么我要用这个而不是真正的方法?
当前回答
在c#中,我们不能像在JavaScript中那样将函数作为参数传递。解决方法是使用委托。
当我们想参数化行为而不是值时,我们使用委托。Lambdas是编写委托的实用语法,可以很容易地将行为作为函数传递。
其他回答
您还可以在编写作用于方法的泛型代码时使用lambda表达式。
例如:计算方法调用所花费的时间的泛型函数。(即这里的动作)
public static long Measure(Action action)
{
Stopwatch sw = new Stopwatch();
sw.Start();
action();
sw.Stop();
return sw.ElapsedMilliseconds;
}
你可以使用lambda表达式调用上述方法,如下所示,
var timeTaken = Measure(() => yourMethod(param));
表达式允许您从方法和out参数中获取返回值
var timeTaken = Measure(() => returnValue = yourMethod(param, out outParam));
匿名函数和表达式对于一次性方法非常有用,这些方法不需要从创建完整方法所需的额外工作中获益。
想想这个例子:
List<string> people = new List<string> { "name1", "name2", "joe", "another name", "etc" };
string person = people.Find(person => person.Contains("Joe"));
与
public string FindPerson(string nameContains, List<string> persons)
{
foreach (string person in persons)
if (person.Contains(nameContains))
return person;
return null;
}
它们在功能上是等价的。
在c#中,我们不能像在JavaScript中那样将函数作为参数传递。解决方法是使用委托。
当我们想参数化行为而不是值时,我们使用委托。Lambdas是编写委托的实用语法,可以很容易地将行为作为函数传递。
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表达式就像代替委托实例编写的匿名方法。
delegate int MyDelagate (int i);
MyDelagate delSquareFunction = x => x * x;
考虑lambda表达式x => x * x;
输入参数值为x(在=>的左边) 函数逻辑为x * x(在=>的右侧)
lambda表达式的代码可以是语句块,而不是表达式。
x => {return x * x;};
例子
注意:Func是一个预定义的泛型委托。
Console.WriteLine(MyMethod(x => "Hi " + x));
public static string MyMethod(Func<string, string> strategy)
{
return strategy("Lijo").ToString();
}
参考文献
委托和接口如何可以互换使用?