Difference between .ToString() and Convert.Tostring() in .NET
The Major Difference between .ToString() and Convert.Tostring() is about NULL Exception.
The ToString() fails Converting object to string when the object is null.
We get error when the Object is NULL.
The Convert.Tostring() handle the NULL Object.
Convert.Tostring()handles null and not throws ObjectReferenceNullException.
Tostring() does not handles the null value and throws the null exception error message.
Here’s an example
.NET C#
public String YourName = null;
protected void Page_Load(object sender, EventArgs e)
{try
{Response.Write(“<b>.ToString</b><br>”);
Response.Write(“FullName :” + FullName.ToString());}
catch (Exception ex)
{Response.Write(ex.Message);
}
Response.Write(“<br><br>”);
try
{Response.Write(“<b>Convert.ToString</b><br>”);
Response.Write(“Your Name :” + Convert.ToString(YourName));}
catch (Exception ex)
{Response.Write(ex.Message);
}
}
The RESULT is :
Tostring()
Object reference not set to an instance to an object.Convert.Tostring()
YourName: