C# 关键字什么时候不保留?
我只是发现自己心不在焉地使用 from
作为标识符。
我意识到可以使用 @
来转义标识符名称并因此使用保留字,但我不明白为什么在这种情况下我没有收到警告或错误。
我不想将保留字用于除其预期目的之外的任何用途,但我不想再次犯类似的错误,并且想知道在某些情况下不保留语言关键字背后的基本原理。
I just found myself absent-mindedly using from
as an identifier.
I realise that it is possible to use @
to escape identifier names and as such use reserved words, but I don't understand why in this case I got no warning or error.
I have no desire to use reserved words for anything but their intended purpose, but I don't want to make a similar mistake again and would like to know the rationale behind having language keywords that are not reserved in certain circumstances.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有些关键字仅在某些上下文中保留,例如
partial class
中的partial
。请参阅MSDN 上的此主题下的“上下文关键字”。
Some keywords are only reserved in certain contexts, e.g., the
partial
inpartial class
.See the "contextual keywords" under this topic on MSDN.
C# 团队特别尝试避免在该语言中创建新的保留关键字。添加的任何新关键字都意味着它会自动破坏使用该关键字作为标识符的现有代码。因此,只要有可能,C# 就会使用上下文关键字来最小化或消除破坏现有代码的可能性。
上下文关键字是在特定上下文中使用时仅是关键字的关键字,例如
from
、partial
、var
等......该上下文不包含标识符:)我不相信自 C# 2.0 以来添加了一个新关键字(甚至不确定 2.0 添加了一个)
The C# team specifically tries to avoid creating new reserved keywords in the language. Any new keyword added means that it automatically breaks existing code which used that keyword as an identifier. Hence whenever possible C# will use a contextual keyword to minimize or eliminate the possibility of breaking existing code.
A contextual keyword is one that is only a keyword when used in a specific context like
from
,partial
,var
, etc ... That context does not include identifiers :)I do not believe there's been a new keyword added since C# 2.0 (not even sure 2.0 added one)
from 是“上下文关键字”。 C# 中的很多这样的东西,只有在特定上下文中使用时才表现得像关键字。最大的优点是向语言中添加这样的上下文关键字不会破坏现有代码。
您将在此MSDN 页面的第二个表中找到它们。是的,来自就在那里。
from is a 'contextual keyword'. Lots of those in C#, they only behave like a keyword when they are used in a certain context. The big advantage is that adding such a contextual keyword to the language won't break existing code.
You'll find them listed in the 2nd table in this MSDN page. Yup, from is there.
当它们与上下文相关时,它们就不是保留的(MSDN 链接):
They are not reserved when they are contextual (MSDN link):
回答根本问题:
C# 1 之后添加的所有关键字都是上下文相关的。这可以避免重大更改。
几乎所有 C# 1 关键字都是保留的,除了访问器关键字(
add
、remove
、get
和set
) 。To answer the underlying question:
All keywords added after C# 1 are contextual. This avoids breaking changes.
Almost all C# 1 keywords are reserved, except for accessor keywords (
add
,remove
,get
, andset
).大多数 C# 关键字始终是保留的。正如您所发现的,有些仅在某些情况下保留。它们被称为上下文关键字。我相信这是更好地确保向后兼容性的一种方法。
原因是
from
在 C# 2 中是完全可以接受的变量名。C# 3 添加了 LINQ,并且需要关键字。为了保持最大的兼容性,添加了from
作为上下文关键字。Most of the C# keywords are always reserved. Some, as you found out, is only reserved in some contexts. They are called contextual keywords. I believe this is a way to better ensure backwards compatibility.
The reasoning is that
from
was a perfectly acceptable variable name in C# 2. C# 3 added LINQ, and they needed keywords for it. In order to maintain maximum compatibility,from
was added as a contextual keyword.