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

Title

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

Introduction

In this post we will see what is difference between Convert.ToString() method and .ToString() method. Convert.ToString() and .ToString() are the major formatting method in the .NET Framework. It converts an object to its string representation so that it is suitable for display.

But the question is why two different methods ? You’ll get answer by the end of this post.

Lets see how these two methods are different from each other.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button runat="server" ID="btn_checkString" Text="Check String" OnClick="btn_checkString_Click" />
    </div>
    </form>
</body>
</html>

Default.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_checkString_Click(object sender, EventArgs e)
    {
        int abc = 1234;
        string a = abc.ToString();

        string b = Convert.ToString(abc);
        Response.Write("Using Convert.ToString() method:- " + a);
        Response.Write("</br>");
        Response.Write("Using .ToString() method:- " + b);
    }
}

In above code, first we’ll be converting and integer number to string using both the method and will see the resultant output.

OUTPUT of above code

1

 

The output shows that both method convert the integer number into string, the question is same. What is the difference between both the methods.

Now lets see another piece of code, instead of number lets now try with using NULL.

Using Convert.ToString() method

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_checkString_Click(object sender, EventArgs e)
    {
        string abc = "";
        abc = null;
        Response.Write("Using Convert.ToString() method: " + Convert.ToString(abc));
    }
}

OUTPUT

2

 

 

 

 

 

Here NULL is displayed as blank.

Using .ToString() method

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_checkString_Click(object sender, EventArgs e)
    {
        string abc = "";
        abc = null;
        Response.Write("Using .ToString() method: " + abc.ToString());
    }
}

 

OUTPUT

3

 

 

 

 

Using .ToString() methods shoots error while trying to convert NULL into String.

So here is the difference between Convert.ToString() and .ToString(), method. Convert.ToString() methods can handle NULL, where as .ToString() method can not handle NULL.


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar