Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal? type is not culture specific

I was converting a decimal to a string, using the following lines of code:

decimal a = 0;
a.ToString();

and Resharper gave me the following warning: "Specify string culture explicity". I guess this makes sense, since some cultures may use a "." (dot) or a "," (comma) as a decimal mark.

However, the following lines of code:

decimal? a = 0; //a is now nullable
a.ToString();

do not present the same warning. I was wondering if this is a bug in Resharper, or is there something special about a nullable decimal?

like image 869
Manuel Reis Avatar asked Jan 07 '23 04:01

Manuel Reis


2 Answers

To extend upon Gama Felix's answer, I would assume that this is because decimal?.ToString() does not have overloads which would accept a culture. If you don't care about culture specific strings, then you're set. If you do care, then you should check for a value and use decimal?.Value.ToString().

like image 89
Glorin Oakenfoot Avatar answered Jan 09 '23 17:01

Glorin Oakenfoot


I believe that it happens because decimal? is just a alias to Nulable<decimal>, so you are calling the ToString() method on the nullable class not the decimal type.

like image 30
Gama Felix Avatar answered Jan 09 '23 19:01

Gama Felix