env: VS2017 v15.9.24, .net framework 2.0 c# console project.
this is a very simple console project, no any reference, all codes are in program.cs:
namespace ConsoleApp1
{
class Program
{
static void Main()
{
var b = new BClass { Prop = new object() };
new AClass(b);
}
}
public class AClass
{
BClass bClass;
public AClass(BClass b)
{
bClass = b;
var a1 = bClass; // not null
var a2 = bClass.Prop; // not null
var a3 = b?.Prop; // not null
var a4 = bClass?.Prop; // null, WHY???
; // set break point to here
}
}
public class BClass
{
public object Prop { get; set; }
}
}
a4
is not actuallynull
, but the debugger displays that it is when you hover over. If you let the execution advance just one line more, the debugger catches up. If you actually try to usea4
, it's correctly not null, even though the debugger shows otherwise. Worth a bug report.a4
is indeed null at the point immediately before executing the next line (which is wrong), but becomes not null when you start executing the next line. I.e. if you havevar a4 = bClass?.Prop; Console.WriteLine(a4);
it will write "System.Object" to the console, but if you put a breakpoint at the WriteLine and executeConsole.WriteLine(a4)
from the immediate window, it will print null to the console. In all cases all variable display windows display null for the value ofa4
.