In one of our application I've come across some lines like this:
Console.WriteLine(string.Empty);
I checked whether it makes any difference if I just write:
Console.WriteLine();
But the output is the same (as expected).
What's "best practice" in this example? Are there any situations where it's necessary to pass an empty string instead of not passing any parameter?
WriteLine() is implemented like this:
public virtual void WriteLine() {
Write(CoreNewLine);
}
WriteLine(string) however is implemented like this:
public virtual void WriteLine(String value) {
if (value==null) {
WriteLine();
}
else {
// We'd ideally like WriteLine to be atomic, in that one call
// to WriteLine equals one call to the OS (ie, so writing to
// console while simultaneously calling printf will guarantee we
// write out a string and new line chars, without any interference).
// Additionally, we need to call ToCharArray on Strings anyways,
// so allocating a char[] here isn't any worse than what we were
// doing anyways. We do reduce the number of calls to the
// backing store this way, potentially.
int vLen = value.Length;
int nlLen = CoreNewLine.Length;
char[] chars = new char[vLen+nlLen];
value.CopyTo(0, chars, 0, vLen);
// CoreNewLine will almost always be 2 chars, and possibly 1.
if (nlLen == 2) {
chars[vLen] = CoreNewLine[0];
chars[vLen+1] = CoreNewLine[1];
}
else if (nlLen == 1)
chars[vLen] = CoreNewLine[0];
else
Buffer.InternalBlockCopy(CoreNewLine, 0, chars, vLen * 2, nlLen * 2);
Write(chars, 0, vLen + nlLen);
}
}
If you call it with a null string, then you get the same result as for WriteLine() without arguments (plus an additional method call). The logic when passing a non-null string however is a bit more complex.
For string.Empty this will allocate a new character array of length 2 and copy the new line character to that.
This is generally not expensive but still somewhat redundant if you don’t want to print anything. Especially for fixed calls to Console.WriteLine it does not make any sense to pass string.Empty there.
You should prefer Console.WriteLine() over Console.WriteLine(string.Empty) if alone for simplicity.
The best practice is to use the more readable Console.WriteLine();.
Even though there's no semantic difference, you'll execute unnecessary code if you write Console.WriteLine(string.Empty);:
public virtual void WriteLine(String value) {
if (value==null) {
WriteLine();
}
else {
// We'd ideally like WriteLine to be atomic, in that one call
// to WriteLine equals one call to the OS (ie, so writing to
// console while simultaneously calling printf will guarantee we
// write out a string and new line chars, without any interference).
// Additionally, we need to call ToCharArray on Strings anyways,
// so allocating a char[] here isn't any worse than what we were
// doing anyways. We do reduce the number of calls to the
// backing store this way, potentially.
int vLen = value.Length;
int nlLen = CoreNewLine.Length;
char[] chars = new char[vLen+nlLen];
value.CopyTo(0, chars, 0, vLen);
// CoreNewLine will almost always be 2 chars, and possibly 1.
if (nlLen == 2) {
chars[vLen] = CoreNewLine[0];
chars[vLen+1] = CoreNewLine[1];
}
else if (nlLen == 1)
chars[vLen] = CoreNewLine[0];
else
Buffer.InternalBlockCopy(CoreNewLine, 0, chars, vLen * 2, nlLen * 2);
Write(chars, 0, vLen + nlLen);
}
/*
Write(value); // We could call Write(String) on StreamWriter...
WriteLine();
*/
}
There's a buffer allocation and string copy in there. It would be better if they had written if (string.IsNullOrEmpty(value)) but for some reason they haven't.
Whereas the simpler way is just:
public virtual void WriteLine() {
Write(CoreNewLine);
}
(CoreNewLine is a char[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With