我已经快速阅读了Microsoft Lambda表达式文档。

不过,这样的例子帮助我更好地理解:

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

不过,我还是不明白为什么这是一种创新。它只是一个在“方法变量”结束时死亡的方法,对吧?为什么我要用这个而不是真正的方法?


当前回答

这只是使用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明显地清理了语法。

其他回答

Lambda清理了c# 2.0的匿名委托语法…例如

Strings.Find(s => s == "hello");

在c# 2.0中是这样完成的:

Strings.Find(delegate(String s) { return s == "hello"; });

在功能上,它们做的是完全相同的事情,只是语法更简洁。

例如,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表达式。这允许你做这样的事情:

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明显地清理了语法。

The biggest benefit of lambda expressions and anonymous functions is the fact that they allow the client (programmer) of a library/framework to inject functionality by means of code in the given library/framework ( as it is the LINQ, ASP.NET Core and many others ) in a way that the regular methods cannot. However, their strength is not obvious for a single application programmer but to the one that creates libraries that will be later used by others who will want to configure the behaviour of the library code or the one that uses libraries. So the context of effectively using a lambda expression is the usage/creation of a library/framework.

此外,由于它们描述的是一次性使用的代码,所以它们不必是类的成员,这样会导致代码更加复杂。想象一下,每当我们想要配置类对象的操作时,都必须声明一个焦点不明确的类。

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();
    }

参考文献

委托和接口如何可以互换使用?