Difference between Convert.ToString() and ToString()
-
Convert.ToString() handle null values but ToString() don't
-
ToString() output as per format supplied
-
Convert.ToString() only handle null values
-
ToString() handle null values but Convert.ToString() don't
The critical difference is null handling: calling ToString() on a null reference throws NullReferenceException, while Convert.ToString(null) returns null without throwing. Convert.ToString() also handles various type conversions via IConvertible, while ToString() is the instance method for string representation.
ToString() is an instance method, so calling it on a null reference throws a NullReferenceException. Convert.ToString() is a static helper that explicitly checks for null and returns string.Empty instead of throwing, which is why it's the safer choice when the object might be null.