[英]C# @ modifier for methods parameters
I was using ReSharper plugin on VS2010 and i was generating an interface method. 我在VS2010上使用ReSharper插件,我正在生成一个接口方法。 ReSharper put an @ on the parameter name.
ReSharper在参数名称上加了@。 WHat is that used for?
那用的是什么?
int Count(Func<ContratoList, bool> @where);
Whats the difference for 什么是差异
int Count(Func<ContratoList, bool> where);
Thanks! 谢谢!
The @
symbol allows you to use reserved words in a variable name. @
符号允许您在变量名中使用保留字 。
int @class = 1;
void MyMethod(int @goto);
bool @public { get; set; }
As Marc correctly pointed out in his comment and his answer, ReSharper is actually wrong to do this because where
is a Contextual Keyword and is not actually a reserved word, so your method will compile without the @
. 正如马克正确他的意见,他的回答中指出,实际上ReSharper的是错误要做到这一点,因为
where
是一个关联关键字的,实际上并不是一个保留字,所以你的方法编译没有@
。
In many ways, resharper is wrong to do this. 在许多方面,resharper做错了。
where
is a contextual keyword, meaning: it only acts as a keyword in some very specific scenarios (ie LINQ). where
是一个上下文关键字,意思是:它只在一些非常特定的场景(即LINQ)中充当关键字。 In the position indicated, it actually does nothing. 在所示的位置,它实际上什么也没做。 It would not be confused as a keyword there, as when the C# language designers add keywords to C# they need to ensure pre-existing code continues to compile (as far as possible), and that would have been legal in earlier C#.
它不会被混淆为关键字,因为当C#语言设计者向C#添加关键字时,他们需要确保预先存在的代码继续编译(尽可能),这在早期的C#中是合法的。
The usage of @
also confuses/complicates some tools (razor, in particular, since razor already uses @
to indicate the start of code - meaning that for a variable using @
(ie @string
) sometimes you need @
and sometimes you need @@
- and I know of at least one false-positive IDE warning this can cause). @
的使用也使某些工具混淆/复杂化(特别是razor,因为razor已经使用@
来表示代码的开头 - 这意味着对于使用@
的变量(即@string
),有时你需要@
,有时你需要@@
- 我知道至少有一个假阳性的IDE警告可能导致这种情况。
However! 然而! If the parameter was
if
or class
etc, then @if
/ @class
allows you to use that as a variable name rather than getting confused as a C# keyword. 如果参数是
if
或class
等,则@if
/ @class
允许您将其用作变量名而不是混淆为C#关键字。 That also isn't a great idea, note. 注意,这也不是一个好主意。 But by the same token, we wouldn't start doing that to all our code (
string @name = ...
etc) - so why do it here? 但出于同样的原因,我们不会开始对我们所有的代码(
string @name = ...
等)这样做 - 所以为什么要在这里呢? It isn't needed, and as demonstrated by this question , it has added confusion. 它不是必需的, 正如这个问题所证明的那样 ,它增加了混乱。
Personally, though, I'd find a parameter name that isn't a keyword or contextual-keyword. 但就个人而言,我会找到一个不是关键字或上下文关键字的参数名称。
它允许您使用保留关键字作为变量名称。
它将停止该参数表现得像一个关键字(如where
是在LINQ关键字除其他事项外)。
这样做是因为'where'是C#(LINQ)中受保护的关键字
它允许您使用保留字作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.