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

回眸只為那壹抹淺笑 提交于 2019-12-03 14:35:47

问题


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));
}

回答1:


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.




回答2:


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/




回答3:


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.




回答4:


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




回答5:


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.




回答6:


To prefix generated things by @ is a usual behaviour in other generators too.
I saw it at least in a web service proxy generated by wsdl.exe. The WSDL named a property protected and the wsdl.exe generated a property with name @protected. So there is no conflict with C# keyword protected.
But I don't know why t is prefixed in your case. Is there a static member in your class which has the name t?



来源:https://stackoverflow.com/questions/9304544/resharper-gives-an-prefix-to-a-variable-name-in-a-lambda-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!