Friday, January 27, 2012

Difference between Convert.ToString() VS .ToString() method


Convert.ToString  handles null and not throws ObjectReferenceNullException. ToString does not handles the null value and throws the null exception error message.


Example:--

    public String FullName = 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("FullName :" + Convert.ToString(FullName));
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

No comments:

Post a Comment