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

2

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.

Volny answered 11/1, 2016 at 8:34 Comment(5)
What is the warning exactly? Please create a Minimal, Complete, and Verifiable example based on your problem.Restaurateur
What is the code exactly?Sedan
This is a good question that I'm asking myself lots of times. But you need to add example code and the exact messageDistrict
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,Honea
Fixed in R# 9.0, apparentlyGabrielgabriela
O
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.

Otterburn answered 11/1, 2016 at 8:40 Comment(3)
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. :)Volny
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.Otterburn
The reference source also shows this to be true.Carabin
M
-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")

Myongmyopia answered 11/1, 2016 at 8:40 Comment(1)
That's not the point here. There is not ToUpper or ToLower methods in this case.Restaurateur

© 2022 - 2024 — McMap. All rights reserved.