为什么 ReSharper 在未明确指定 CultureInfo 时会在 Char.ToString() 处发出警告?

Why does ReSharper warn at Char.ToString() when not specifying CultureInfo explicitly?

我想知道为什么在我尝试将字符转换为字符串而不提供特定区域性信息时 ReSharper 会警告我。

有没有可能在两个系统上进行不同转换的情况?

示例:

var str = ' '.ToString();

默认会弹出如下ReSharper警告:

Specify a culture in string conversion explicitly.

我找到了这个http://csharpindepth.com/Articles/General/Strings.aspx

Some of the oddities of Unicode lead to oddities in string and character handling. Many of the string methods are culture-sensitive - in other words, what they do depends on the culture of the current thread. For example, what would you expect "i".toUpper() to return? Most people would say "I", but in Turkish the correct answer is "İ" (Unicode U+0130, "Latin capital I with dot above")

这是因为 ReSharper 发现该类型实现了 IConvertible,它具有 ToString(IFormatProvider).

System.Char 本身不会公开具有该签名的 public 方法,即使 documentation 表明它公开:

如果您查看带有 IFormatProvider 参数的重载,您将看到此通知:

Implements
IConvertible.ToString(IFormatProvider)

还有这句话:

The provider parameter is ignored; it does not participate in this operation.

ReSharper 只是注意到该方法的存在,以及在没有 IFormatProvider 的情况下对 ToString 的调用,因此会发出抱怨,在这种情况下,您可以安全地忽略它。