z包含NULL。为什么输出窗口不显示NULL?是因为NULL不是值吗?
int? x = null;
int? y = 1;
int? z = 0;
z = x + y;
Debug.WriteLine(z);
答案 0 :(得分:6)
当您查看reference source of Console.WriteLine(Object)
时,您会看到.NET调用System.IO.TextWriter.WriteLine(Object)
,它会明确检查null
,然后输出一个空行:
public virtual void WriteLine(Object value) {
if (value==null) {
WriteLine();
}
else {
// Call WriteLine(value.ToString), not Write(Object), WriteLine().
// This makes calls to WriteLine(Object) atomic.
IFormattable f = value as IFormattable;
if (f != null)
WriteLine(f.ToString(null, FormatProvider));
else
WriteLine(value.ToString());
}
}
Debug.WriteLine(Object)
的行为方式相同,但有点复杂。它最终调用了System.Diagnostics.TraceListener.WriteLine(Object)
。