Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is null not written to output window?

Tags:

c#

null

output

z contains NULL. Why doesn't the output window show the NULL? Is it because NULL is not a value?

int? x = null;
int? y = 1;
int? z = 0;
z = x + y;
Debug.WriteLine(z);

Output window does not show null

like image 857
Robert Vogelezang Avatar asked Dec 07 '25 06:12

Robert Vogelezang


1 Answers

When you look into the reference source of Console.WriteLine(Object), you can see that .NET calls System.IO.TextWriter.WriteLine(Object) which does explicitly check for null and will then output a blank line:

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) behaves the same way but is a bit more complicated. It eventually calls System.Diagnostics.TraceListener.WriteLine(Object).

like image 129
nkr Avatar answered Dec 09 '25 19:12

nkr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!