Click here to Skip to main content
15,896,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
.ToString()
Convert.ToString()
We all knows first case will throw a null exception. While second will execute normaly.And its best practice is always to use second case.
Why we have first case at all? when we can do everything with second case?
Why .ToString() is designed in .net?

Please help me out.
Posted
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 20:50pm    
Best practice is to use first case, provided it compiles (.ToString()) will not.
--SA

.ToString() surely will throw a null exception if the object you are calling it on is Null? chances are if you are calling it on a null object, then something else has gone wrong further up the stack.

Also many types or custom classes can override the ToString for custom implementations, so like many things, its up to the programmer to decide how they best want to make use of what is available to them.

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 21:01pm    
And Convert will throw the same exception in the same situation -- what to talk about?
Object.ToString his is one of the fundamental OOP methods. It's System.Object.ToString -- enough said.
Please see my answer.
--SA
ToString is the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display.
ToString is should be used when you have a need to change a data type to a string. For built in types like int and such there string representations are what you expect hence we can say that ToString allows you to use the complex formating build into .net with your own objects. you can provide complex formatters like the datetime class does to give flexibility in using your own types.

Take a look at given MSDN Links Object.ToString Method[^]
Convert.ToString Method[^]
for more details
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Jul-11 21:00pm    
Correct, my 5. This is not just formatting, this is one of the fundamental OOP method. It's System.Object.ToString -- enough said.
Please see my answer.
--SA
RaviRanjanKr 3-Jul-11 7:47am    
Thanks SA :)
Why Convert class is designed in .NET?

We all know that Convert.ToString(null) will throw a null exception. Why having this method is myObject.ToString() will always return a correct result if myObject was instantiated. Why having the first form at all? :-)

Seriously, ToString is a fundamental method of all objects. It is fundamentally important as it is used inside the .NET libraries internally everywhere a string representation is needed. This method is object-oriented, it is applied on polymorphous containers.

In contrast, the class Convert does not have fundamental importance. It's mainly a collection of utility methods mostly used for convenience of the developers. I notices this class is mostly used by very inexperienced developers. Not all of it, though. Some methods are really important, such as group of the method ChangeType and base64 methods. In general, this is just a collection of primitive methods put together for convenience.

—SA
 
Share this answer
 
Comments
sushil_gupta 2-Jul-11 23:51pm    
How to give up votes? As we have option to give down votes only?...
Sergey Alexandrovich Kryukov 3-Jul-11 0:37am    
Up-vote means a vote of 4 (good enough) and 5 (excellent).
Down-vote means 1 (lie, stupid, irrelevant, misleading), 2 (bad).
I'm not sure about 3 -- somewhere in between.

If you think an answer/advice makes sense, please formally accept it (green button). You can accept more than one.

Thank you.
--SA
RaviRanjanKr 3-Jul-11 7:47am    
Perfect Answer, have a my 5 too :)
Sergey Alexandrovich Kryukov 3-Jul-11 12:29pm    
Thank you, Ravi.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900