C# Action lambda 代码块的限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/234239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limitations of C# Action lambda code blocks
提问by Matt
C# .NET 3.5. I'm trying to understand the intrinsic limitation of the C# Action object. Within the lamda (are those, in fact, lamdas?), we can perform assignments, call functions, even execute a ternary operation, but we can't execute a multi-statement operation.
C# .NET 3.5。我试图了解 C# Action 对象的内在限制。在 lamda(实际上是那些 lamdas 吗?)中,我们可以执行赋值、调用函数,甚至执行三元运算,但我们不能执行多语句运算。
Is this because the single-statement execution is just syntactic sugar for wrapping it in a delegate? Why does the first example below not work?
这是因为单语句执行只是将其包装在委托中的语法糖吗?为什么下面的第一个例子不起作用?
public class MyClass
{
private int m_Count = 0;
public void Test()
{
int value = 0;
// Does not work, throws compile error
Action action = () => { if(m_Count < 10) m_Count++; value = m_Count; }
// Works
Action action2 = () => value = delegate(){
if(m_Count < 10)
m_Count++;
return m_Count;
};
// Works
Action action3 = () => value = m_Count;
// Works
Action action4 = () => value = m_Count < 10 ? m_Count++ : 0;
// Works
Action action5 = () => value = Increment();
}
public int Increment()
{
if (m_Count < 10)
m_Count++;
return m_Count;
}
}
EDIT: Grr, sorry for the noise. Originally, I had
编辑:Grr,对于噪音很抱歉。原来,我有
Action action = () => if(m_Count < 10) m_Count++; value = m_Count;
Which threw a compile error, but then right before the post I thought I'd try wrapping it in braces
这引发了编译错误,但就在发布之前,我想我会尝试将其包装在大括号中
Action action = () => { if(m_Count < 10) m_Count++; value = m_Count; }
Which also threw a compile error, so I jumped to conclusions that it was the same problem. It works, though, if I toss in a semi-colon after the braces
这也引发了编译错误,所以我得出结论,这是同样的问题。但是,如果我在大括号后加上分号,它会起作用
Action action = () => { if(m_Count < 10) m_Count++; value = m_Count; };
Sorry for the noise!
对噪音表示抱歉!
EDIT 2: Thanks cfeduke, you posted that at the same time as my edit above - went ahead and marked as answer.
编辑 2:谢谢 cfeduke,你在我上面的编辑的同时发布了 - 继续并标记为答案。
采纳答案by cfeduke
You are missing a semi-colon, it compiles:
你缺少一个分号,它编译:
Action action = () => { if (m_Count < 10) m_Count++; value = m_Count; };
When you say type name = statement;
you need a semicolon even if you use braces for a code block.
当您说type name = statement;
即使您对代码块使用大括号也需要分号时。
回答by Jon Skeet
cfeduke has posted the solution to getting your code to compile.
cfeduke 已经发布了让您的代码进行编译的解决方案。
Note that you can't convert statement-block lambda expressions into expression trees, even though you can convert them into delegates. There are other limitationson what you can convert into expression trees.
请注意,您不能将语句块 lambda 表达式转换为表达式树,即使您可以将它们转换为委托。对于可以转换为表达式树的内容还有其他限制。
Going back to delegates, there are somelimitations there - you can't write an iterator block within a lambda expression, for example. (I've wanted to do that before now - it gets weird when you try to get your head round it. You can't do it though.) For the most part, you can do almost anything you can do in a method.
回到委托,那里有一些限制 - 例如,您不能在 lambda 表达式中编写迭代器块。(我以前一直想这样做 - 当你试图绕过它时会变得很奇怪。但你不能这样做。)在大多数情况下,你几乎可以在一个方法中做任何你能做的事情。