- Published on
Difference Between "Convert.ToString()" and "ToString()" Method in C#
Overview
In C#, both the Convert.ToString()
method and the ToString()
method are used to convert a value to its string representation. However, there are some key differences between the two methods. This article will explore these differences and provide examples to illustrate their usage.
Convert.ToString()
The Convert.ToString()
method is a static method provided by the Convert
class in C#. It can be used to convert various types, including value types and reference types, to their string representations. The Convert.ToString()
method handles null values by returning an empty string instead of throwing a NullReferenceException
.
Here's an example that demonstrates the usage of Convert.ToString()
:
int number = 10;
string numberString = Convert.ToString(number);
Console.WriteLine(numberString); // Output: "10"
double doubleNumber = 3.14;
string doubleString = Convert.ToString(doubleNumber);
Console.WriteLine(doubleString); // Output: "3.14"
DateTime date = DateTime.Now;
string dateString = Convert.ToString(date);
Console.WriteLine(dateString); // Output: "5/8/2023 12:34:56 PM"
string nullString = Convert.ToString(null);
Console.WriteLine(nullString); // Output: ""
In the example above, the Convert.ToString()
method is used to convert an int
, double
, DateTime
, and a null
value to their respective string representations. Note that the method returns an empty string when given a null value.
ToString()
The ToString()
method is a virtual method that is defined by the System.Object
class. Since all types in C# implicitly inherit from System.Object
, the ToString()
method is available for all objects in C#.
By default, the ToString()
method returns the fully qualified name of the object's type. However, most types override this method to provide a more meaningful string representation.
The ToString()
method can throw a NullReferenceException
if called on a null object.
Here's an example that demonstrates the usage of ToString()
:
int number = 10;
string numberString = number.ToString();
Console.WriteLine(numberString); // Output: "10"
DateTime date = DateTime.Now;
string dateString = date.ToString();
Console.WriteLine(dateString); // Output: "5/8/2023 12:34:56 PM"
object obj = new object();
string objString = obj.ToString();
Console.WriteLine(objString); // Output: "System.Object"
int? nullableNumber = null;
string numberString = nullableNumber.ToString(); // Throws NullReferenceException
In the example above, the ToString()
method is called on an int
, DateTime
, and an object
. The method returns the string representation specific to each type. Also, ToString()
is called on nullable integer variable that are assigned null value. Since the ToString()
method is called on null objects, it results in a NullReferenceException
being thrown.
Key Differences
The key differences between Convert.ToString()
and ToString()
can be summarized as follows:
Convert.ToString()
is a static method, whileToString()
is an instance method.Convert.ToString()
can handle null values and returns an empty string, whereasToString()
can throw aNullReferenceException
if called on a null object.Convert.ToString()
can be used with various types, including value types and reference types, whileToString()
is available for all objects but can be overridden to provide a custom string representation.
Conclusion
In conclusion, both Convert.ToString()
and ToString()
are used to convert values to their string representations in C#. However, the choice between the two methods depends on the specific scenario. Use Convert.ToString()
when dealing with different types and when null values need to be handled gracefully. On the other hand, use ToString()
when working with objects and when a custom string representation is required.