本文翻译自:What's the use/meaning of the @ character in variable names in C#?
I discovered that you can start your variable name with a '@' character in C#. 我发现可以在C#中以'@'字符开头变量名。 In my C# project I was using a web service (I added a web reference to my project) that was written in Java. 在我的C#项目中,我正在使用用Java编写的Web服务(我向项目添加了Web引用)。 One of the interface objects defined in the WSDL had a member variable with the name "params". WSDL中定义的接口对象之一具有一个名为“ params”的成员变量。 Obviously this is a reserved word in C# so you can't have a class with a member variable with the name "params". 显然,这是C#中的保留字,因此您不能有一个带有成员变量的类,其名称为“ 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. 我搜索了VS 2008 c#文档,但找不到任何相关信息。 Also searching Google didn't give me any useful answers. 另外,搜索Google并没有给我任何有用的答案。 So what is the exact meaning or use of the '@' character in a variable/property name? 那么,变量/属性名称中“ @”字符的确切含义或用途是什么?
#1楼
参考:https://stackoom.com/question/nSV/C-中变量名中-字符的用途-含义是什么
#2楼
Unlike Perl's sigils, an @
prefix before a variable name in C# has no meaning. 与Perl的信号不同,C#中变量名之前的@
前缀没有任何意义。 If x
is a variable, @x
is another name for the same variable. 如果x
是变量,则@x
是同一变量的另一个名称。
> string x = "abc";
> Object.ReferenceEquals(x, @x).Dump();
True
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. 但是,正如您所发现的, @
前缀确实有一个use -您可以使用它来澄清C#否则会拒绝视为非法的变量名称。
> string string;
Identifier expected; 'string' is a keyword
> string @string;
#3楼
Another use-case is in extension methods. 另一个用例是在扩展方法中。 The first, special parameter can be distinguished to denote its real meaning with @this
name. 可以区分第一个特殊参数,以@this
名称表示其实际含义。 An example: 一个例子:
public static TValue GetValueOrDefault<TKey, TValue>(
this IDictionary<TKey, TValue> @this,
TKey key,
TValue defaultValue)
{
if (!@this.ContainsKey(key))
{
return defaultValue;
}
return @this[key];
}
#4楼
You can use it to use the reserved keywords as variable name like 您可以使用它来将保留关键字用作变量名,例如
int @int = 3;
the compiler will ignores the @
and compile the variable as int
编译器将忽略@
并将变量编译为int
it is not a common practice to use thought 用思想不是普遍的做法
#5楼
The @
symbol allows you to use reserved keywords for variable name. @
符号允许您使用保留关键字作为变量名。 like @int
, @string
, @double
etc. 像@int
, @string
, @double
等。
For example: 例如:
string @public = "Reserved Keyword used for me and its fine";
The above code works fine, but below will not work: 上面的代码工作正常,但下面的代码无效:
string public = "This will not compile";
#6楼
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 字符@实际上不是标识符的一部分,因此该标识符在其他语言中可能被视为普通标识符,没有前缀