string
what is the diff between string,and convert.string
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Nov 7, 2012, 7:10 AM
Convert.ToString handles null and not throws ObjectReferenceNullException. ToString does not handles the null value and throws the null exception error message.
C#
public String FullName = null;
protected void Page_Load(object sender, EventArgs e)
{
try
{
Response.Write(".ToString
");
Response.Write("FullName :" + FullName.ToString());
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Response.Write("
");
try
{
Response.Write("Convert.ToString
");
Response.Write("FullName :" + Convert.ToString(FullName));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Vithal WadjePosted Nov 7, 2012, 6:56 AM
Sandeep Singh ShekhawatPosted Nov 7, 2012, 6:52 AM
ToString() raise exception when the object is null
So in the case of object.ToString(), if object is null, it raise NullReferenceException.
Convert.ToString() return string.Empty in case of null object
(string) cast assign the object in case of null
So in case of
MyObject o = (string)NullObject;
But when you use o to access any property, it will raise NullReferenceException.