variable.ToString() and Convert.ToString(variable) both are used for converting data into string. But the main difference is that Convert.ToString() handles null, while ToString() doesn't handle null value.
Apart from that both works almost same. Let’s take an example to explain it:
- double amount = 10000.0;
- Console.WriteLine(amount.ToString());
- Console.WriteLine(Convert.ToString(amount));
In this case both method will convert “
amount” to string and will return the same value but in case of null value variable.ToString() method will throw exception.
Now take another example:
- object myObject = null;
- string myString;
-
-
-
-
- myString = Convert.ToString(myObject);
311