[This post is part of a series, "wish-list for future versions of VB"]


IDEA: Introduce a syntax for null-propagating field access. We could use the syntax ".?" for this. For instance,

        ' What the user writes

        Dim name = Customer.?Name


        ' The compiler translates it into this:

        Dim $temp = Customer

        Dim name = If($temp Is Nothing, Nothing, $temp.Name)


Incidentally, null-propagating already occurs in many parts of VB. For instance,

        Dim x As Integer? = Nothing

        Dim y = x + 5  ' "Nothing" propagates from "x" across + operator into "y"


        Dim xml = <xml><b>hello</b></xml>

        Dim p = xml...<a>

        Dim q = p.@href  ' "Nothing" propagates from "p" across the .@ operator into "q"


Note that we have to be careful about concurrency when we do null-propagating field access. If we merely wrote "Dim name = If(Customer is Nothing, Nothing, Customer.Name)" then it's vulnerable if another thread sets Customer to Nothing after the test has passed but before we retrieved Customer.Name. That's why it's important to store Customer into $temp.


Provisional evaluation from VB team: This is cute, but doesn't seem worth adding new (cryptic) syntax to VB just to support this case.