String
what is the diff between string,and convert.string
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted Nov 7, 2012, 1:52 AM EST
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.
Vithal WadjeRank #19 · 47.2K reputationPosted Nov 7, 2012, 1:56 AM EST
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 7, 2012, 2:10 AM EST
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);
}
}