C# 变量名中 @ 字符的用途/含义是什么?
我发现您可以在 C# 中以“@”字符开头变量名。 在我的 C# 项目中,我使用了用 Java 编写的 Web 服务(我向我的项目添加了 Web 引用)。 WSDL 中定义的接口对象之一有一个名为“params”的成员变量。 显然,这是 C# 中的保留字,因此您不能拥有一个包含名为“params”的成员变量的类。 生成的代理对象包含一个如下所示的属性:
public ArrayList @params {
get { return this.paramsField; }
set { this.paramsField = value; }
}
我搜索了 VS 2008 c# 文档,但找不到任何相关内容。 搜索谷歌也没有给我任何有用的答案。 那么变量/属性名称中“@”字符的确切含义或用途是什么?
I discovered that you can start your variable name with a '@' character in C#.
In my C# project I was using a web service (I added a web reference to my project) that was written in Java. One of the interface objects defined in the WSDL had a member variable with the name "params". Obviously this is a reserved word in C# so you can't have a class with a member variable with the name "params". The proxy object that was generated contained a property that looked like this:
public ArrayList @params {
get { return this.paramsField; }
set { this.paramsField = value; }
}
I searched through the VS 2008 c# documentation but couldn't find anything about it. Also searching Google didn't give me any useful answers. So what is the exact meaning or use of the '@' character in a variable/property name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用它来使用保留关键字作为变量名,就像
编译器会忽略
@
并将变量编译为int
这不是使用思想的常见做法
You can use it to use the reserved keywords as variable name like
the compiler will ignores the
@
and compile the variable asint
it is not a common practice to use thought
如果我们使用关键字作为标识符的名称,则会出现编译器错误“需要标识符,‘标识符名称’是关键字”
要克服此错误,请在标识符前加上“@”前缀。 此类标识符是逐字标识符。
字符 @ 实际上并不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀
If we use a keyword as the name for an identifier, we get a compiler error “identifier expected, ‘Identifier Name’ is a keyword”
To overcome this error, prefix the identifier with “@”. Such identifiers are verbatim identifiers.
The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix
另一个用例是扩展方法。 第一个特殊参数可以通过
@this
名称来区分以表示其真正含义。 一个例子:Another use case are extension methods. The first, special parameter can be distinguished to denote its real meaning with
@this
name. An example:它只是允许您使用保留字作为变量名。 前几天我想要一个名为
event
的 var。 我原本打算使用_event
来代替,但我的同事提醒我,我可以将其命名为@event
。It simply allows you to use reserved words as variable names. I wanted a var called
event
the other day. I was going to go with_event
instead, but my colleague reminded me that I could just call it@event
instead.@
符号允许您使用变量名称的保留关键字。 如@int
、@string
、@double
等。例如:
上面的代码可以正常工作,但下面的代码将不起作用:
The
@
symbol allows you to use reserved keywords for variable name. like@int
,@string
,@double
etc.For example:
The above code works fine, but below will not work:
与 Perl 的符号不同,C# 中变量名前的
@
前缀没有任何意义。 如果x
是变量,则@x
是同一变量的另一个名称。但正如您所发现的,
@
前缀确实有一个use - 您可以使用它来澄清 C# 会拒绝的变量名称,否则这些变量名称是非法的。Unlike Perl's sigils, an
@
prefix before a variable name in C# has no meaning. Ifx
is a variable,@x
is another name for the same variable.But the
@
prefix does have a use, as you've discovered - you can use it to clarify variables names that C# would otherwise reject as illegal.在 C# 中,at (@) 字符用于表示明确不遵守语言规范中相关规则的文字。
具体来说,它可以用于与保留关键字冲突的变量名(例如,您不能使用 params,但可以使用 @params 代替,与语言规范中的 out/ref/任何其他关键字相同)。 此外,它还可用于未转义的字符串文字; 这与路径常量特别相关,例如,您可以编写
path = @"c:\temp\somefile.txt 而不是
。 它对于正则表达式也非常有用。path = "c:\\temp\\somefile.txt"
“In C# the at (@) character is used to denote literals that explicitly do not adhere to the relevant rules in the language spec.
Specifically, it can be used for variable names that clash with reserved keywords (e.g. you can't use params but you can use @params instead, same with out/ref/any other keyword in the language specification). Additionally it can be used for unescaped string literals; this is particularly relevant with path constants, e.g. instead of
path = "c:\\temp\\somefile.txt"
you can writepath = @"c:\temp\somefile.txt"
. It's also really useful for regular expressions.它只是允许您使用保留字作为变量名。 恕我直言,不推荐(除非像你这样的情况)。
It just lets you use a reserved word as a variable name. Not recommended IMHO (except in cases like you have).
直接来自 C# 语言规范,< a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specation/lexical-struct#identifiers" rel="noreferrer">标识符 (C#)
:
Straight from the C# Language Specification, Identifiers (C#)
: