Search code examples
c#stringcastingconverterstostring

In C#, which one is faster — Convert.ToString(), (string)casting or .ToString()?


In C#, which one is faster?

  • System.Convert.ToString(objThatIsAString)
  • (string)objThatIsAString
  • objThatIsAString.ToString()

Solution

  • The direct cast doesn't have to do any checking except a runtime type check - I would expect that the cast is quicker.

    You might also want to consider objThatIsString.ToString(); since (for string) this is just return this;, it should be quick - certainly quicker than the Convert.ToString. Then the race is between a runtime type-check and a virtual call; in reality, both are very, very quick.