Back in college one of my profs. taught us to just do x + \"\"
as a quick conversion from basic types to strings.
I don\'t remember which class it was in I
There are no advantages or disadvantages of using x+""
. toString
is the method of Object
type which implicitly called every time you add any object to string.
You can always override this method in your class if you'd like.
In C# I would completely disregard this advice in favor of something more declarative such as using .ToString
directly.
The only potential advantage this syntax provides you is it will create an empty string if x
is null
. I would favor an extension method if this is considered an advantage in your code.
public static string NullSafeToString<T>(this T value) where T : class {
return value == null ? "" : value.ToString();
}
The other reason to avoid this is because it can create precedence confusion. For example do you know exactly what the value of z
is in the following scenario without the use of reference materials?
int x = 42;
int y = 15;
string z = x + y + "";
Now same question with the following
int x = 42;
int y = 15;
string z = x + y.ToString();
The latter is more likely to be understood at a glance by the average developer who hasn't taken the time to memory C# operator precedence. Hence I would prefer it because it has a lesser chance of being misunderstood.
Because strings are immutable, x+""
invokes two functions: x.toString() and StringBuffer.append() (in Java, at least). I imagine most good JITs would simply turn that into a single function call, x.toString(), but I couldn't be sure without actually testing.
It doesn't matter, really. It is very very unlikely this will cause any performance problems whatsoever. So it remains a matter of personal style. Use whatever you or your team is comfortable with.
Honestly, I consider that kind of weird advice.
I can't speak to every specific case, but in general what x + ""
will do in C# (which should depend on the existence of an overloaded +
operator for either the type of x
or string
) is call something like string.Concat(x, "")
which in turn will invoke x.ToString
anyway.
In the typical case, this just means that x + ""
has the overhead of one more method call than x.ToString
. When x
is a variable of some value type, however, this can also cause the value of x
to be boxed unless an overload for +
exists specifically for the type of x
(this might be considered a useless point to make, as x
will also be boxed in a call to ToString
if its type has not overridden that method; this strikes me a a bit rarer, but it most assuredly does happen).
These are fairly trivial differences, of course. The real difference between these two approaches is that of readability; in my experience, x + ""
is not very idiomatic in .NET and so I would be inclined to avoid it. That said, it could just be that it isn't common in the slice of the .NET world I inhabit, while there could be plenty of .NET developers out there who do it.
I will point out that while in Java, perhaps you had to write the unwieldy Integer.toString(x)
for variables x
of primitive types like int
, in C# and in .NET in general all types (including so-called "primitive" ones) inherit from object
and so have the method ToString
(along with GetType
and GetHashCode
) available to them.
In Java,
In addition to working with primitive types, x+""
also works with null
. Where x.toString()
throws a NullPointerException
, x+""
returns "null"
. Whether that is better or not is up to you, but it is a difference.