J
Jon Skeet [C# MVP]
DeveloperX said:I agree it's type safe, but the former passes an int and a string as
object parameters and the latter two strings to string parameters.
Given the choice I'd rather use the most accurate signature available
for what I want to do, in this case concat two strings. Ok, string
concatenation probably isn't the best example as it's really simple
and I do take your point on readability. I personally think I prefer
the ToString version as it's obvious that I want the int's string
representation. Imagine this example:
int i=1;
int j=2;
Console.WriteLine("My value is: " + i + j);
Console.WriteLine("My value is: " + (i + j));
Console.WriteLine(i + j);
Suddenly I get
My value is: 12
My value is: 3
3
So if a developer simply removed the string literal the intention of
the line of code changes.
I would never use the first form - always the second or third. They're
both perfectly readable, IMO, and I think string concatenation is
sufficiently "part of the language" that it's obvious that when I'm
concatenating a string and an int, that I want the string
representation of the int.