DeveloperInToronto
DeveloperInToronto

Reputation: 1193

Convert.ToString(...) or Object.ToString() for performance

Suppose I have a loop which is going to convert an ArrayList with 10 million elements, filled with int, to an array of string. Should I be using Convert.ToString(...) or Object.ToString()? Is it true that in this case Convert.ToString(...) unboxes the elements and decreases the performance?

Upvotes: 4

Views: 1971

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502206

If you've got an ArrayList, any value types will already be boxed. Why are you using ArrayList rather than a List<int>? The latter will avoid both the execution time cost of boxing and the significant space implications.

After changing to use List<int> however, I'd then just call ToString. It says exactly what you want to do in a simpler way than Convert.ToString, IMO... and provides more formatting options.

Upvotes: 10

Related Questions