C# 关键字什么时候不保留?

发布于 2024-10-06 09:21:15 字数 193 浏览 2 评论 0原文

我只是发现自己心不在焉地使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

×眷恋的温暖 2024-10-13 09:21:15

有些关键字仅在某些上下文中保留,例如partial class 中的partial

请参阅MSDN 上的此主题下的“上下文关键字”。

Some keywords are only reserved in certain contexts, e.g., the partial in partial class.

See the "contextual keywords" under this topic on MSDN.

妄司 2024-10-13 09:21:15

C# 团队特别尝试避免在该语言中创建新的保留关键字。添加的任何新关键字都意味着它会自动破坏使用该关键字作为标识符的现有代码。因此,只要有可能,C# 就会使用上下文关键字来最小化或消除破坏现有代码的可能性。

上下文关键字是在特定上下文中使用时仅是关键字的关键字,例如 frompartialvar 等......该上下文不包含标识符:)

我不相信自 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)

枕头说它不想醒 2024-10-13 09:21:15

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.

云之铃。 2024-10-13 09:21:15

当它们与上下文相关时,它们就不是保留的(MSDN 链接):

上下文关键字用于
提供特定含义
代码,但它不是保留字
C#。一些上下文关键字,例如
部分和哪里,有特殊
在两个或多个上下文中的含义。

They are not reserved when they are contextual (MSDN link):

A contextual keyword is used to
provide a specific meaning in the
code, but it is not a reserved word in
C#. Some contextual keywords, such as
partial and where, have special
meanings in two or more contexts.

謌踐踏愛綪 2024-10-13 09:21:15

回答根本问题:

C# 1 之后添加的所有关键字都是上下文相关的。这可以避免重大更改。

几乎所有 C# 1 关键字都是保留的,除了访问器关键字(addremovegetset) 。

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, and set).

丢了幸福的猪 2024-10-13 09:21:15

大多数 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文