Hello freinds,
Recently i was interviewes in accenture and i was asked what the difference between tostring and convertostring. I think tostring checks for spaces and convertostring does not

Dani AI

Generated

Quick practical clarification (builds on , and ): ToString() is an instance method (declared on System.Object and commonly overridden), while Convert.ToString(...) is a static helper with several overloads. That difference (instance vs static + overloads) is the root of the behavior people notice. (learn.microsoft.com)

Important overload nuance you’ll see in real code — this often surprises people:

object o = null;
string a = Convert.ToString(o);      // returns String.Empty (object overload)
string b = o.ToString();             // throws NullReferenceException

string s = null;
string c = Convert.ToString(s);      // returns null (string overload — input unchanged)
string d = Convert.ToString((object)s); // returns String.Empty (object overload used)

The static Convert.ToString(object) explicitly returns String.Empty for a null object; the Convert.ToString(string) overload simply returns the string reference it was given (so null stays null). (learn.microsoft.com) (learn.microsoft.com)

Practical recommendations for code and interviews:

  • If you want an explicit empty string when the source might be null, use obj?.ToString() ?? "" or Convert.ToString((object)obj) (but be aware of the overload difference above).
  • If you need culture-aware formatting for user-facing output, use the Convert.ToString(object, IFormatProvider) or the type’s ToString(IFormatProvider) overloads. (learn.microsoft.com)

Short interview answer: mention instance vs static, explain the null/overload nuance, and give the safe coding pattern (?.ToString() ?? "") as a concrete recommendation.

Its not spaces its nulls.
convert.tostring handles nulls while simple tostring() function does not handle null.

Are you sure about it khadak...

Nice video to the point thanks a lot.

Tostring:-
Its not handle the null values.Its throwing error.
Convert to string:-
Its handle null value also.Its not throwing an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.