.net c#可以将局部变量命名为yield

A local variable can be named as yield
2021-09-14
  •  译文(汉语)
  •  原文(英语)

刚发现

foreach (int yield in items) {
  yield return yield * 2;
}

或者

int yield = 10; 

是C#中的有效代码.

我可以理解,对于编译器来说,区分变量产量和迭代者产量可能很容易,但是仍然增加了混乱并降低了代码的可读性.

我们知道允许这样做的确切原因吗?

速聊1:
我相信,事情还会逆转.首先,建立有关变量命名的规则,然后禁止使用某些关键字.因此,不应有任何理由允许某些事情.您知道拒绝yield作为变量名的任何正当理由吗?
解决过程1

我们知道允许这样做的确切原因吗?

是的.yield仅仅是上下文关键字.它是C#2中引入的,团队不想破坏已经用作yield标识符的代码.

从1.0开始引入的其他上下文关键字也是如此partial.有关完整列表,请参见Eric Lippert的博客文章.

但它仍然增加了混乱并降低了代码的可读性

那你就不要那样做吗?您可以做很多事情来使您的代码真正,非常难以阅读.您可以使所有变量名都带有下划线:

private int _;
private string __;
// etc

你会那样做吗?如果不是,您为什么期望人们yield专门编写令人困惑的代码?

速聊1:
为了保持任何向后兼容性,我们是否应该将c#中的关键字列表视为已关闭?
速聊2:
一点也不.为了避免歧义,需要将某些关键字设置为非上下文.
速聊3:
还:部分,动态,添加,删除...?(尽管添加/删除为1.0)
速聊4:
: 是的,我想是这样.我会惊讶地发现添加了新的"常规"关键字.
速聊5:
C#团队不遗余力地确保添加新关键字是上下文相关的,特别是出于向后兼容的原因. yield return本来可以yield作为一个完整的关键字,但这将是一个巨大的变化.他们在上下文中避免了这种情况. await如果一个人做,那将是一个巨大的改变;async之所以添加此代码,是因为两者在一起都是上下文相关的,并且没有重大变化,这与单独运行不一样await.
解决过程2

yield是上下文关键字.它可以是一个普通关键字,但是他们选择将其作为上下文关键字.

上下文关键字的主要原因是向后兼容.如果他们在该语言中添加了普通关键字,则可能会破坏从该语言的旧版本复制的代码.

例如,想象一下在yield作为普通关键字添加后编译此代码:

bool yield = false;
// bla bla

效果不是很好,是吗?您必须重命名变量,然后才能使用较新版本进行编译.

Just found out

foreach (int yield in items) {
  yield return yield * 2;
}

or

int yield = 10; 

are valid codes in C#.

I can understand it could be easy for the compiler to differentiate between a variable yield and an iterator yield, but still it adds to the confusion and lessen the readability of the code.

Do we know the exact reasons why is it allowed?

Talk1:
I believe, things are other way around. First, rules on variable naming are established, then some keywords are banned from usage. So, there should not be any reason to allow something. Do you know any good reason to reject yield as a variable name?
Solutions1

Do we know the exact reasons why is it allowed?

Yes. yield is only a contextual keyword. It was introduced in C# 2, and the team didn't want to break code which already used yield as an identifier.

The same is true for other contextual keywords which have been introduced since 1.0, such as partial. See Eric Lippert's blog post for the complete list.

but still it adds to the confusion and lessen the readability of the code

So don't do that then? There are any number of things you can do to make your code really, really hard to read. You could make all your variable names just underscores:

private int _;
private string __;
// etc

Do you do that? If not, why would you expect people to write confusing code with yield specifically?

Talk1:
In order to preserve any backward compatibility, shall we consider the list of the keywords in c# as closed?
Talk2:
Not at all. Some keywords need to be made non-contextual, to avoid ambiguity.
Talk3:
also: partial, dynamic, add, remove, ...? (Although add/remove are 1.0)
Talk4:
: Yes, I think so. I would be surprised to see new "normal" keywords added.
Talk5:
The C# team has gone out of their way to ensure that new keywords are added are contextual specifically for backwards compatability reasons. yield return could have been just yield as a full keyword, but that would be a breaking change. They made it contextual to avoid that. await would have been a breaking change if it was alone; async was added because with the two together they're both contextual, and there are no breaking changes, unlike if await was on it's own.
Solutions2

yield is a contextual keyword. It could be an ordinary keyword, but they chose to make it a contextual keyword.

The main reason for contextual keywords is backwards compatibility. If they added a normal keyword to the language, it might break code that is being copied from older versions of the language.

For example, imagine compiling this code after yield was added as a normal keyword:

bool yield = false;
// bla bla

It wouldn't work very well, would it? You'd have to rename your variables before you could compile it with the newer version.

转载于:https://stackoverflow.com/questions/15576150/a-local-variable-can-be-named-as-yield

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入