1

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; }
    }
}
6
  • Looks like a debugging artifact. a4 is not actually null, 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 use a4, it's correctly not null, even though the debugger shows otherwise. Worth a bug report.
    – GSerg
    Commented Jun 14, 2020 at 9:40
  • Or rather, 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 have var a4 = bClass?.Prop; Console.WriteLine(a4); it will write "System.Object" to the console, but if you put a breakpoint at the WriteLine and execute Console.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 of a4.
    – GSerg
    Commented Jun 14, 2020 at 9:46
  • No, it is also null in a real project at runtime, causing a problem, so I discovered this problem.
    – ahdung
    Commented Jun 14, 2020 at 9:46
  • Upgrading the framework version seems to fix it.
    – GSerg
    Commented Jun 14, 2020 at 9:47
  • 1
    Reported at github.com/dotnet/roslyn/issues/45165
    – GSerg
    Commented Jun 14, 2020 at 10:07

1 Answer 1

1

A strange question about Null-conditional?

Actually, the issue is related to the old net framework 2.0.

We have also tested the same issue as you described. Since we cannot do anything here, I have reported this issue on our DC Forum. See my link.

You can vote it , add any detailed info or comment here if I did not elaborate on the problem.

Anyone who is interested in this issue will preview it and then vote it so that it will attract great attention from Microsoft.

2
  • thank you, i voted, and i reported another one too use VS feedback, but i can't get the url, how do you get it?
    – ahdung
    Commented Jun 17, 2020 at 1:28
  • 1
    You can enter this link and then log in your account and then you can see the link under My Feedback from your account.
    – Mr Qian
    Commented Jun 17, 2020 at 2:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.