When to use ToString() and when not to

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

Hi,

If I have code like
int i = 10;
string s = "Number is " + i.ToString();

FxCop wants me to put an IFormatProvider to ToString().

If I have:
int i = 10;
string s = "Number is " + i;

What is going on there? ToString() must be called implicitly, right? So what
IFormatProvider is being passed to the implicit call to ToString()?

I'm changing my ToString() calls because of FxCop. I'm basically adding
ToString(CultureInfo.CurrentCulture) or
ToString(CultureInfo.InvariantCulture). I'm also considering not calling
ToString() when not explicitly needed; like in the 2 lines code above.

Thanks,
Michael
 
Hi Michael,

It is very simple. It there is any kind of string in the concatenation, then all objects that aren't strings will automatically call its own ToString() method. If there are no strings, then you need to call ToString() on all objects yourself.

string s = i; // error
string s = "" + i; // ok
 
ToString() is good "as such" mostly for debugging.
Or to store data in a file in a language (culture) independent way.
This is because most of the strings returned by toString
are not localized and using this in a localized UI will be bad.
FxCop wants me to put an IFormatProvider to ToString(). ....
I'm changing my ToString() calls because of FxCop. I'm basically adding
ToString(CultureInfo.CurrentCulture) or
ToString(CultureInfo.InvariantCulture). I'm also considering not calling
ToString() when not explicitly needed; like in the 2 lines code above.
Internationalization is exactly the reason why FxCop asks you to do this.
This will force you to decide if you need a culture sensitive format or not.

For instance in most of Europe the comma is used as a decimal separator:
3.14 should be displayed as 3,14
And the dot (or non-breaking space, in France) is the thousand separator:
1,234.56 should be displayed as 1.234,56 (or 1 234,56)

But if you use it for serialization, you want the to use InvariantCulture
for read and write, so that your English application can read a file
produced by the German one.
 
Morten Wennevik said:
It is very simple. It there is any kind of string in the
concatenation, then all objects that aren't strings will
automatically call its own ToString() method. If there are no
strings, then you need to call ToString() on all objects yourself.

I think Michael's aware of that, and merely asking why FxCop thinks
it's okay for the compiler to call ToString() itself, but not for him
to explicitly call it without any parameters.
 
Ah, indeed

There was a few "newbie" questions around, and I wasn't familiar with FxCop so I couldn't tell the significant of it.

Just installed FxCop. Seems like a useful tool. I notice the program calls itself Microsoft FxCop, is it really Microsoft supported?
 
Morten Wennevik said:
Ah, indeed

There was a few "newbie" questions around, and I wasn't familiar with
FxCop so I couldn't tell the significant of it.

Just installed FxCop. Seems like a useful tool. I notice the program
calls itself Microsoft FxCop, is it really Microsoft supported?

Not sure I'd call it supported as such - it's not something you'd go to
your Microsoft rep about, you'd just post on the FxCop message board -
but the team who wrote it are in MS.
 
Back
Top