When using ReSharper it automatically adds an @
, why?
public static string RemoveDiacritics(this string input)
{
if (string.IsNullOrEmpty(input)) return input;
var normalizedString = input.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var value in normalizedString.Select(value =>
new {value, unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(value)})
.Where(@t => @t.unicodeCategory != UnicodeCategory.NonSpacingMark)
.Select(@t => @t.value)) stringBuilder.Append(value);
return (stringBuilder.ToString().Normalize(NormalizationForm.FormC));
}
The @ symbol allows you to use a reserved keyword for a variable name. Such as @class
. I'd assume Resharper does this to be safe.
In this case, it is not needed and it doesn't have any effect.
You would have to ask the resharper implementers to be certain, but I can make an educated guess. They are probably future proofing.
In the time since C# 1.0 shipped the compiler team has added 21 new contextual keywords to C#; the compiler treats these as keywords when they appear in certain locations, and as ordinary identifiers otherwise. yield
, for example, is only a keyword when it appears before return
.
When the resharper tool generates code for you, they do not know whether that code is going to be compiled in some hypothetical C# 6 that uses t
as a contextual keyword in some context. So they "future proof" the design by pre-emptively calling out "this identifier is not a contextual keyword" by putting the @
on front of it.
Incidentally, this is precisely why it is legal for any identifier to be prefixed with @
.
More information here:
http://ericlippert.com/2009/05/11/reserved-and-contextual-keywords/
Just some context, in ReSharper 5 there was a bug where this:
groups.Select(group => ...)
would be turned into this
from group in groups ...
Now, group is a Keyword in LINQ Query Syntax, so R#5's refactoring actually broke the code. In R#6 this was apparently fixed using @ for identifiers.
The at-sign (@) escapes the names. E.g. if you want to use if
as variable name you can write
int @if;
if
alone would not work, since if
is a c# keyword.
The @
in front of t
is useless here. Probably the person who wrote that is using his private naming conventions and uses it to denote lambda parameters.
(OK, I see, it was Resharper, not a person, however it could have been).
I've only seen @
used in this one refactoring: Convert LINQ to Method Chain. In this case, ReSharper is creating a number of lambda variables (possibly a large number, depending on the complexity of the lambda expression being converted).
One guess as to the reason is that they might have deliberately used something ugly in the hopes that you'll replace them with meaningful names. There aren't really many hints that ReSharper could use to guess at a meaningful name, so it's up to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With