Lambda 解释和它是什么以及一个很好的例子

发布于 2024-08-22 17:44:37 字数 65 浏览 5 评论 0原文

谁能给我一个关于如何使用 Lambda 的很好的解释并给出一个很好的例子。我见过它,但我不知道它是什么或有什么作用。

Can anyone give me a good explanation of how to use Lambda and give a good example. I have seen it but I dont know what it is or does.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶色山野 2024-08-29 17:44:38

Lambda 只是一个委托,它是一个匿名函数,您可以创建该函数以供以后执行。

Lambda 表达式是表达式树形式的未编译委托,您可以在编译和执行之前对其进行操作。

http://msdn.microsoft.com/en-us/library/bb397687。 ASPX

A Lambda is simply a delegate, its an anonymous function that you can create for later execution.

A Lambda Expression is an uncompiled delegate in the form of an Expression Tree that you can manipulate before compiling and executing.

http://msdn.microsoft.com/en-us/library/bb397687.aspx

涙—继续流 2024-08-29 17:44:38

也许我有点简单化了,但是,如果我是你,首先我会认为 lambda 是一种通过删除嵌套 foreach 循环或前 n 个元素等内容来缩短代码的好方法。

因此,如果您在酒店周围寻找一些便宜的房间,您可以(假设 IEnumerable 中的酒店):

cheapHotels = hotels.Where(h => h.PriceFrom < 50)

一旦开始点击,您就可以转向更复杂的东西,这是一种随机方法,我可以在我当前的项目中找到使用lambdas(可能是从其他地方偷来的!):

private T DeserializeObject<T>(XmlDocument xDoc, string typeName)
{
    Type type = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Single(t => t.FullName == typeName);

    object o;
    var serializer = new XmlSerializer(typeof(T));
    using (TextReader tr = new StringReader(xDoc.InnerXml))
    {
        o = serializer.Deserialize(tr);
        tr.Close();
    }
    return (T)o;

}

Perhaps I'm being a bit simplistic, but, if I were you, to start with I'd just consider lambdas as a nice way to shorten code by removing things like nested foreach loops or top n elements.

So if you're running round hotels to find some with cheap rooms you could (assuming hotels in IEnumerable):

cheapHotels = hotels.Where(h => h.PriceFrom < 50)

Once this starts to click you can move onto something more complex, this is a random method that I can find in my current project using lambdas (probably nicked from somewhere else!):

private T DeserializeObject<T>(XmlDocument xDoc, string typeName)
{
    Type type = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Single(t => t.FullName == typeName);

    object o;
    var serializer = new XmlSerializer(typeof(T));
    using (TextReader tr = new StringReader(xDoc.InnerXml))
    {
        o = serializer.Deserialize(tr);
        tr.Close();
    }
    return (T)o;

}
你的呼吸 2024-08-29 17:44:37

lambda 表达式用于创建匿名函数。这里将匿名函数分配给委托变量:

Func<int, int> increase = (a => a + 1);

然后您可以使用委托来调用该函数:

var answer = increase(41);

通常使用 lambda 表达式将委托发送给方法,例如将委托发送给 ForEach方法,以便为列表中的每个元素调用它:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);

list.ForEach(n => Console.WriteLine(n));

A lambda expression is used to create an anonymous function. Here an anonymous function is assigned to a delegate variable:

Func<int, int> increase = (a => a + 1);

You can then use the delegate to call the function:

var answer = increase(41);

Usually lambda expressions are used to send a delegate to a method, for example sending a delegate to the ForEach method so that it's called for each element in the list:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);

list.ForEach(n => Console.WriteLine(n));
_失温 2024-08-29 17:44:37

我不久前发表了一篇文章,希望对您有所帮助: http: //www.dontcodetired.com/blog/?tag=/lambda+表达式

I did a post a while back which I hope may be of some use: http://www.dontcodetired.com/blog/?tag=/lambda+expressions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文