473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

When to use ToString() and when not to

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
Nov 16 '05 #1
5 5206
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

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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.

--
Mihai
-------------------------
Replace _year_ with _ to get the real email
Nov 16 '05 #3
Morten Wennevik <Mo************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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?

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
Morten Wennevik <Mo************@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joseph A Romeo | last post by:
I have written an XSLT transformation on an ASP.NET page. The resulting HTML is primarily a table of links. I have found that when the resulting HTML is less than or equal to 16040 bytes, the...
0
by: Giulio Santorini | last post by:
Hi, I've got two drop down list controls. When I select a value from the first one I fill the second one. And when I select a value from the second one I would like to fill another control. But...
5
by: Dennis Myrén | last post by:
Hi. Is there a way to make sure that float, double and decimal data types never will be presented in a scientific notation? I have tried to round(Math.Round) float's to 7 decimals, double's to...
2
by: Alpha | last post by:
I have a window application. In one of the form, a datagrid has a dataview as its datasource. Initial filtering result would give the datavew 3 items. When I double click on the datagrid to edit...
1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
15
by: Brady Love | last post by:
I am currently working an an app that will post and edit blogs on blogger. Right now I have it so I can recive a list of the blogs and the content of those blogs using Atomizer. This is my first...
5
by: Matt | last post by:
Creating Dynamic aspnet link buttons with in user control, Then i use the control on ever page with wraped with master page. I get the url of the link buttons from xml. Parameter from xml also...
0
by: selvialagar | last post by:
i did a program in vc++.net for ICMP communication using sockets.But when i execute it i received a very slow response...is there any configuration to be made on the server..or any other thing?.. i...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.