C# Lambda 令人费解的行为
Func<Classification, string> test1 = c => c.Id = "x";
Func<Classification, string> test2 = c => { return c.Id = "x";};
我已经使用 lambda 工作了近一年左右,并且对它们相当合理,但今天我正在查看 NBuilder 并看到一个奇怪的 Func ,它似乎与示例不匹配。无论如何我都玩过并且它检查了但我不明白为什么上面的编译更不用说运行了。我们正在做一个作业,因此表达式不会计算出任何值,对吗???所以
我想也许我错过了与 lambda 相关的东西,所以我尝试了其他方法:
[Test]
public void AmIGoingMad()
{
Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?
}
public string Test()
{
string subject = "";
return subject = "Matt";
}
果然 AmIGoingMad
失败了,并且实际上返回了“Matt”。
为什么我们会有这种行为?这是在哪里记录的?它纯粹是一个语法快捷方式吗?
我觉得我错过了对 lambda 甚至 C# 的一些基本理解。
感觉很蠢。
Func<Classification, string> test1 = c => c.Id = "x";
Func<Classification, string> test2 = c => { return c.Id = "x";};
I've worked with lambda's for nearly a year or so now and fairly reasonable with them, but today I was looking at NBuilder and seen a weird Func that didn't seem to match the examples. I had play anyway and it checks out but I don't understand why the above compiles let alone runs. We are doing an assignment and thus the expression doesn't evaluate to anything, right??? or not
So I thought maybe something I've missed related to lambda, so I tried something else:
[Test]
public void AmIGoingMad()
{
Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?
}
public string Test()
{
string subject = "";
return subject = "Matt";
}
Sure enough AmIGoingMad
fails and "Matt" is actually returned.
Why do we have this behavior? Where is this documented? Is it purely a syntactic shortcut?
I feel like I missed something fundamental in my understanding of lambda or even C#.
Feeling dumb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
赋值语句有一个返回值——该值就是被赋值的值。即使 C 也有这个,所以您可以将赋值链接在一起,如下所示:
对 d 的赋值的返回值是 10,该值被赋值给 c,依此类推。
An assignment statement has a return value--that value is that which was assigned. Even C had this, so you could chain together assignments like the following:
The assignment to d has the return value of 10 which gets assigned to c, and so on.
您看到的是赋值链,这可以追溯到 C/C++。它是为了支持这种情况:
或者在我实际使用它的地方:
What you're seeing is assignment chaining, something that goes way back to C/C++. It's there to support this scenario:
Or somewhere I actually use it:
它之所以有效,是因为赋值
c.Id = "x"
的计算结果为“x”。例如,如果您想在一个语句中分配和检查一个值(有些人认为这是不好的做法),则可以使用它,如下所示:It works because the assignment
c.Id = "x"
evaluates to the value of "x". You can use this for example if you want to assign and check a value in one statement (which some people consider bad practice) like this:正如其他人所说,赋值本身返回一个值。
您还可以像这样利用它:
As others said, the assignment returns a value itself.
You could also take advantage of it like this:
我不明白你的问题。
您发布的代码看起来像是误解了
=
和==
之间的区别,然后明确评论说您希望它使用=
这是赋值运算符。让我问你一个问题:
为什么你想要或期望它不能编译?
基本上,你正在这样做:
I don't understand your question.
You post code that looks like you've misunderstood the difference between
=
and==
, and then explicitly comment that you want it to use=
which is the assignment operator.Let me ask you a question instead:
Why would you want or expect this to not compile?
Basically, you're doing this: