ReSharper 给出了一个“@” lambda 表达式中变量名的前缀

发布于 2025-01-06 09:53:04 字数 669 浏览 1 评论 0原文

使用 ReSharper 时它会自动添加 @,为什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深爱不及久伴 2025-01-13 09:53:04

@ 符号允许您使用变量名的保留关键字。例如@class。我认为 Resharper 这样做是为了安全。

在这种情况下,不需要它,也没有任何效果。

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.

情归归情 2025-01-13 09:53:04

您必须询问 resharper 实施者才能确定,但​​我可以做出有根据的猜测。它们可能是面向未来的。。

自 C# 1.0 发布以来,编译器团队已向 C# 添加了 21 个新的上下文关键字;当它们出现在某些位置时,编译器将它们视为关键字,否则视为普通标识符。例如,yield 仅当出现在 return 之前时才是一个关键字。

当 resharper 工具为您生成代码时,他们不知道该代码是否将在某些假设的 C# 6 中编译,该 C# 6 在某些上下文中使用 t 作为上下文关键字。因此,他们通过在其前面放置 @ 来预先指出“该标识符不是上下文关键字”,从而“面向未来”设计。

顺便说一句,这正是任何标识符以@前缀都是合法的原因。

更多信息请参见:

http://ericlippert.com/2009/05/11/reserved-and -上下文关键字/

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/

转身泪倾城 2025-01-13 09:53:04

只是一些上下文,在 ReSharper 5 中存在一个错误,其中 this:

groups.Select(group => ...)

会变成这样

from group in groups ...

现在,group 是 LINQ 查询语法中的关键字,因此 R#5 的重构实际上破坏了代码。在 R#6 中,这显然是使用 @ 作为标识符来解决的。

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.

平定天下 2025-01-13 09:53:04

at 符号 (@) 对名称进行转义。例如,如果你想使用 if 作为变量名,你可以

int @if;

单独编写 if 是行不通的,因为 if 是 ac# 关键字。

t前面的@在这里没有用。编写该代码的人可能正在使用他的私有命名约定并用它来表示 lambda 参数。

(好吧,我明白了,那是雷沙珀,不是一个人,但它本来可以是)。

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

可是我不能没有你 2025-01-13 09:53:04

我只看到 @ 在这一重构中使用:将 LINQ 转换为方法链。在本例中,ReSharper 创建了许多 lambda 变量(可能是一个很大的数字,具体取决于要转换的 lambda 表达式的复杂性)。

对其原因的一种猜测是,他们可能故意使用一些难看的东西,希望您能用有意义的名称替换它们。 ReSharper 可以用来猜测有意义的名称的提示并不多,所以这取决于您。

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.

好多鱼好多余 2025-01-13 09:53:04

在其他生成器中用 @ 作为生成的东西前缀也是常见的行为。
我至少在 wsdl.exe 生成的 Web 服务代理中看到了它。 WSDL 将属性命名为 protected,并且 wsdl.exe 生成了一个名为 @protected 的属性。所以与C#关键字protected没有冲突。
但我不知道为什么 t 在您的情况下带有前缀。您的类中是否有名为 t 的静态成员?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文