在我们的一个应用程序中,我遇到了如下几条线:
Console.WriteLine(string.Empty);我检查了一下,如果我只写:
Console.WriteLine();但是输出是相同的(如预期的)。
本例中的“最佳实践”是什么?是否有必要传递空string而不传递任何参数的情况?
发布于 2016-07-18 09:07:20
WriteLine()的实现方式如下:
public virtual void WriteLine() {
Write(CoreNewLine);
}然而,WriteLine(string)是这样实现的:
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);
}
}如果使用null字符串调用它,则得到与没有参数的WriteLine()相同的结果(外加一个额外的方法调用)。然而,传递非空字符串时的逻辑要复杂一些。
对于string.Empty,这将分配一个长度为2的新字符数组,并将新的行字符复制到该字符。
这通常并不昂贵,但如果你不想打印任何东西的话,它还是有点多余的。特别是对于对Console.WriteLine的固定调用,在那里传递string.Empty没有任何意义。
为了简单起见,您应该更喜欢Console.WriteLine()而不是Console.WriteLine(string.Empty),如果只是简单的话。
发布于 2016-07-18 09:07:59
最佳实践是使用更具可读性的Console.WriteLine();。
即使没有语义上的差异,如果编写不必要的代码,也会执行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();
*/
}里面有一个缓冲区分配和字符串拷贝。如果他们编写了if (string.IsNullOrEmpty(value))会更好,但是由于某种原因他们没有写。
而更简单的方法是就是
public virtual void WriteLine() {
Write(CoreNewLine);
}(CoreNewLine是char[])
发布于 2016-07-18 09:04:21
在技术方面,我没有任何区别。这正是开发人员感到舒适和可读性更强的方式。对我来说,Console.WriteLine()更直观地认为它是在写空行,而Console.WriteLine(String.Empty)让我停下来思考一下(也许更少)。
任何其他的变化,海事组织,是一种行为,使代码的花哨,而不使用在专业的生产代码。
https://stackoverflow.com/questions/38432513
复制相似问题