Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper gives an "@" prefix to a variable name in a lambda expression

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));
}
like image 998
Javier Ramírez Avatar asked Feb 16 '12 02:02

Javier Ramírez


5 Answers

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.

like image 56
Joel Avatar answered Nov 17 '22 18:11

Joel


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/

like image 22
Eric Lippert Avatar answered Nov 17 '22 17:11

Eric Lippert


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.

like image 4
Michael Stum Avatar answered Nov 17 '22 17:11

Michael Stum


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).

like image 2
Olivier Jacot-Descombes Avatar answered Nov 17 '22 17:11

Olivier Jacot-Descombes


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.

like image 2
John Saunders Avatar answered Nov 17 '22 18:11

John Saunders