Difference between convert.tostring() and .ToString() in C#.Net

Friday, October 26, 20121comments

In this article we will discuss what is the difference between convert.tostring() and .tostring() in C#.Net. You can also check my previous articles on how to split a string using javascript, how to remove html tags from string in C#.Net and What is difference between String and StringBuilder in C#.Net.

The major difference between convert.tostring() and .tostring() is Convert.ToString() handles null value but ToString() doesn't handle null value, means whenever you will try to convert null to string using .ToString it will through an Null reference exception.

.ToString() raise exception when the object is null
Convert.ToString() return string.Empty in case of null object


int age=0;
string strAge= Convert.ToString(age); //This will not throw any execption and will convert it to string.

string strAge= age.ToString(); //This will not handle null value and eill throw null reference exception.
Share this article :

+ comments + 1 comments

Saturday, November 03, 2012 12:18:00 PM

Even my project is of C#.net i have faced problem while i am involved in to the sting part!Thanks for this as i want to preserve the data!

Post a Comment

Copyright © 2010. SharePointDotNet.com - All Rights Reserved
Template Created by Creating Website Published by Mas Template