Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Saturday 11 February 2017

Difference Between ToString() and Convert.ToString()

This article is about the Difference Between ToString and Convert.ToString Method in C#.

ToString and Convert.ToString Method in C#

Both methods have the ability to convert a value to a string. The main difference between the two methods is that ToString() method can’t handle Null values, so if you use this method, you will experience a Null Reference Exception in your code at some point in time (as shown below Example). On the other hand Convert.ToString() method can handle Null values. So, as a good coding practice and to be on the safe side, experts recommend always use Convert.ToString() method.

We have used below mentioned examples to demonstrate the difference between ToString and Convert.ToString methods.

.ToString() Example:

In C#, if you declare a string variable and do not assign any value to the variable, the variable takes a null value by default. In this case, if you use the ToString() method, the program will raise a null reference exception.


 using System;

namespace DemoToString
{
    public class Program
    {
        static void Main(string[] args)
        {
            Object objStr = null;
            //Below line will throw NullReference Exception
            //Because .ToString() can't handle Null values
            string val = objStr.ToString(); 
            Console.WriteLine(val);
            Console.ReadLine();
        }
    }
}
tostring-and-convert-tostring

When we run the program it will give us null reference exception because the ToString() method in C# expects that the object cannot be NULL when called on it. In our example, the object objStr is Null, and we call ToString() on the NULL object, so it gives a NULL Reference exception.

Convert.ToString() Example:

Let's see what happens when we utilize the Convert.Tostring() method in the above example. Now, after making the changes, run the program, and it should execute correctly. So to put it plainly, the Convert.ToString() method handles null, and the ToString() method does not deal with Null and tosses an exception.


using System;

namespace DemoConvertToString
{
    public class Program
    {
        static void Main(string[] args)
        {
            Object objStr = null;
            //Below line will Return Null Or Blank
            string val = Convert.ToString(objStr);
            Console.WriteLine(val);
            Console.ReadLine();
        }
    }
}

Convert is a static class in the .Net System namespace, and if you noticed that when you write.ToString() It is in a dark blue color(which means that this method is only available in C#) and Convert.ToString() is in light blue color which means it is global and available in other .Net languages as well.

I will go through the difference between object, var, and dynamic keywords through some examples in my next article. Today, I have tried to explain the difference between .tostring and convert.tostring methods. I trust this article will assist you with your requirements. I hope to receive your feedback. Please post your criticism, questions, or remarks on this article.

No comments:

Post a Comment

Please do not enter spam links.