8

I was wondering why ReSharper does warn me, when I'm trying to convert a char to a string without giving a specific culture info.

Is there any case, where it could be converted differently on two systems?

Example:

var str = ' '.ToString();

The following ReSharper warning will pop up by default:

Specify a culture in string conversion explicitly.

Jannik
  • 2,310
  • 6
  • 32
  • 61
  • What is the warning exactly? Please create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) based on your problem. – Soner Gönül Jan 11 '16 at 08:36
  • What is the code exactly? – Tim Schmelter Jan 11 '16 at 08:37
  • This is a good question that I'm asking myself lots of times. But you need to add example code and the exact message – Ghasem Jan 11 '16 at 08:39
  • I suppose the warning fires on all classes/structs that have an overload which accepts `CultureInfo`. Since a IFormatProvider could return anything based on the input, it may be the case that it *should* return something different, – jessehouwing Jan 11 '16 at 08:41
  • [Fixed in R# 9.0, apparently](https://youtrack.jetbrains.com/issue/RSRP-372088) – AakashM Jan 12 '16 at 10:47

2 Answers2

10

This is because ReSharper sees that the type implements IConvertible which has ToString(IFormatProvider).

System.Char by itself does not expose a public method with that signature, even though the documentation indicates it does:

Char.ToString overloads

If you look at the overload with the IFormatProvider parameter you will see this notice:

Implements
IConvertible.ToString(IFormatProvider)

and this remark:

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

ReSharper just notices the presence of that method, and the call to ToString without a IFormatProvider and thus complains, in this case you can safely disregard it.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I like this answer, but it does not answer my (second) question directly yet: Are there any sideeffects between different systems disregarding it. I'd like to have a direct conclusion under all the information you gave me. :) – Jannik Jan 11 '16 at 08:51
  • 2
    No, "you can safely disregard it". A single character will be converted to a string with a single character, there's no conversion happening here. – Lasse V. Karlsen Jan 11 '16 at 08:53
  • The [reference source](http://referencesource.microsoft.com/#mscorlib/system/char.cs,9f693c2a17b40531) also shows this to be true. – Matthew Watson Jan 11 '16 at 09:02
-2

I found this 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")

tdat00
  • 810
  • 1
  • 8
  • 11