为什么赋值语句会返回一个值? [英] Why do assignment statements return a value?

查看:25
本文介绍了为什么赋值语句会返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是允许的:

int a, b, c;
a = b = c = 16;

string s = null;
while ((s = "Hello") != null) ;

据我所知,赋值 s = "Hello"; 应该只会导致 Hello" 被赋值给 s,但是操作不应该返回任何值.如果这是真的,那么 ((s = "Hello") != null) 会产生一个错误,因为 null 会被比作什么都没有.

To my understanding, assignment s = "Hello"; should only cause "Hello" to be assigned to s, but the operation shouldn’t return any value. If that was true, then ((s = "Hello") != null) would produce an error, since null would be compared to nothing.

允许赋值语句返回值的原因是什么?

What is the reasoning behind allowing assignment statements to return a value?

推荐答案

据我所知,assignment s = "Hello";应该只使Hello"分配给 s,但操作不应返回任何值.

To my understanding, assignment s = "Hello"; should only cause "Hello" to be assigned to s, but the operation shouldn’t return any value.

您的理解 100% 不正确.你能解释一下你为什么相信这个错误的东西吗?

Your understanding is 100% incorrect. Can you explain why you believe this false thing?

允许赋值语句返回值的原因是什么?

What is the reasoning behind allowing assignment statements to return a value?

首先,赋值语句不会产生值.赋值表达式产生一个值.赋值表达式是一个合法的声明;在 C# 中只有少数表达式是合法的语句:表达式的等待、实例构造、增量、减量、调用和赋值表达式可以在需要语句的地方使用.

First off, assignment statements do not produce a value. Assignment expressions produce a value. An assignment expression is a legal statement; there are only a handful of expressions which are legal statements in C#: awaits of an expression, instance construction, increment, decrement, invocation and assignment expressions may be used where a statement is expected.

在 C# 中只有一种表达式不会产生某种值,即调用类型为返回 void 的内容.(或者,等效地,一个没有关联结果值的任务的等待.)所有其他类型的表达式都会产生一个值、变量、引用、属性访问或事件访问,等等.

There is only one kind of expression in C# which does not produce some sort of value, namely, an invocation of something that is typed as returning void. (Or, equivalently, an await of a task with no associated result value.) Every other kind of expression produces a value or variable or reference or property access or event access, and so on.

请注意,所有作为语句合法的表达式都对它们的副作用很有用.这是这里的关键见解,我认为这可能是您的直觉的原因,即赋值应该是语句而不是表达式.理想情况下,我们每个语句只有一个副作用,并且表达式中没有副作用. 有点奇怪,副作用代码完全可以在表达式上下文中使用.

Notice that all the expressions which are legal as statements are useful for their side effects. That's the key insight here, and I think perhaps the cause of your intuition that assignments should be statements and not expressions. Ideally, we'd have exactly one side effect per statement, and no side effects in an expression. It is a bit odd that side-effecting code can be used in an expression context at all.

允许此功能背后的原因是因为 (1) 它通常很方便 (2) 它在类 C 语言中是惯用的.

The reasoning behind allowing this feature is because (1) it is frequently convenient and (2) it is idiomatic in C-like languages.

有人可能会注意到有人提出了这个问题:为什么这在类 C 语言中是惯用的?

One might note that the question has been begged: why is this idiomatic in C-like languages?

不幸的是,丹尼斯·里奇 (Dennis Ritchie) 无法再提问了,但我的猜测是,赋值几乎总是将刚刚分配的值留在寄存器中.C 是一种非常接近机器"的语言.似乎有道理并且与 C 的设计保持一致,有一个语言特性基本上意味着继续使用我刚刚分配的值".为此功能编写代码生成器非常容易;您只需继续使用存储分配值的寄存器即可.

Dennis Ritchie is no longer available to ask, unfortunately, but my guess is that an assignment almost always leaves behind the value that was just assigned in a register. C is a very "close to the machine" sort of language. It seems plausible and in keeping with the design of C that there be a language feature which basically means "keep on using the value that I just assigned". It is very easy to write a code generator for this feature; you just keep on using the register that stored the value that was assigned.

这篇关于为什么赋值语句会返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆