Add null-propagating member-access operator (?.) to VB.NET
I've heard a similar feature is possibly in the pipeline for C#, and would love to see it in VB.NET as well. I'd love to reduce:
If points IsNot Nothing Then
Dim next = points.FirstOrDefault
If next IsNot Nothing AndAlso next.X IsNot Nothing Then
Return If(points?.FirstOrDefault()?.X, -1)
It is a very common problem to have to write code such as...
If Order IsNot Nothing AndAlso Order.Customer IsNot Nothing Then cname = Order.Customer.Name
What would be really useful would be to have a special member access operator that would return Nothing/null when the object that you are accessing is null. eg. if we used the pipe character...
cname = Order|Customer|Name
This would set cname to Nothing if either Order Is Nothing or Order.Customer Is Nothing. If neither of them are, it would return Order.Customer.Name
This feature has been added in Visual Basic 14 and is available as part of Visual Studio 2015. You can read about this and other new features for Visual Basic developers here – https://msdn.microsoft.com/en-us/library/vstudio/we86c8x2(v=vs.140).aspx
-Anthony D. Green, Program Manager, Visual Basic
Mike C commented
I could imagine GetList?(0)?.Property, which is workable but does move away from the "spirit" of VB a little, as the sequence of characters starts to look more like a regex or something. (Not that I'd typically write a method that returns Nothing when a list is expected; I'd rather return an empty list.)
However, I don't have any insight in how null-propagation would work with the Await keyword.