I've got some code which works surprisingly well.
When I use the MessageBox.Show command, sometimes I can get a away without using the To String() method. Anybody can say why this snippet of code works ok please :-
decimal averageIteration = iterationsTotal / iterations.Length;
int maxIterations = iterations.Max();
MessageBox.Show("The average number of iterations needed to win are " + averageIteration + "\n" +
"The High number was " + maxIterations + "\n" +
"The Lowest number was " + iterations.Min());
The code above does not have any trouble outputting an int or decimal types. No need to even use the ToString() method.
Thanks to anyone who can help answer this for me
Cheers
Matt
VulpesPosted Nov 12, 2012, 7:12 AM
When this is done, the virtual ToString() method is called on the actual type of the object and then this is concatenated with the string operand.
So, if you have this:
string result = "number " + 5;
the second operand, 5, is an object whose actual type is int. So, int.ToString() which is "5" is concatenated with "number " to give a result of "number 5".
So, this is why it works - and a lot of developers use it - but whether it's as clear as calling ToString() explicitly is another matter. Also, there is a slight run time penalty because the int needs to be 'boxed' on the heap before its ToString() method can be called.