简体   繁体   English

C# 中的 Lambda 示例

[英]Lambda Example in C#

I am still learning some of the features of C# 3.0 and want to know if the following can be reduced to a lambda expression.我还在学习 C# 3.0 的一些功能,想知道以下是否可以简化为 lambda 表达式。

var SomeObject = Combo.EditValue;
var ObjectProperty = SomeObject.Property;

To obtain ObjectProperty from the combo.editvalue in a single line?在一行中从combo.editvalue 中获取ObjectProperty?

Also, if you can provide me with any good references to Lambda expressions, it would be appreciated.另外,如果您能向我提供有关 Lambda 表达式的任何好的参考资料,我们将不胜感激。

EDIT: Ok, the answers posted are great, it appears that the example does not need a Lambda to satisfy the solution.编辑:好的,发布的答案很好,看来该示例不需要 Lambda 来满足解决方案。 I will take a look at the reference links though.不过,我会看看参考链接。

You don't really need lambdas to do that all you would need to do is你真的不需要 lambdas 来做到这一点,你需要做的就是

var ObjectProperty = Combo.EditValue.Property;

I'm not sure a lambda is going to make that any more readable for you.我不确定 lambda 会让你更容易阅读。

Here are some books you might want to take a look at to learn Lambdas in more detail, and also why you'd use them:以下是一些您可能想要阅读的书籍,以更详细地学习 Lambda,以及为什么要使用它们:

More Effective C#更有效的 C#

C# In Depth深入了解 C#

MSDN Reference MSDN 参考

Combining those into one line, you run the risk of a NullReferenceException, by checking the Property property on EditValue.通过检查 EditValue 上的 Property 属性,将这些合并为一行,您将面临 NullReferenceException 的风险。 :) But, here is a really great tutorial on C# 3.0 and functional programming. :) 但是,这里有一个关于 C# 3.0 和函数式编程的非常棒的教程

this does not appear to need a lambda.这似乎不需要 lambda。
Can't you just use你不能只用

var ObjectProperty = Combo.EditValue.Property

As far as lambda references try 101 LINQ Examples for starters.至于 lambda 引用,请尝试101 个 LINQ 示例作为初学者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM