我注意到这篇关于如何对嵌套对象/属性进行内联空值检查的好文章:C# elegant way to check if a property's property is null
是否有可用的VB.NET?
答案 0 :(得分:1)
是。 null条件运算符(MSDN)也存在于VB.NET(VB 14及更高版本,即Visual Studio 2015及更高版本)中,并具有相同的语法:
Dim value As Int32? = objectA?.PropertyA?.PropertyB?.PropertyC
通常,这与null-coalescing运算符结合使用,在C#中为a ?? b
,在VB.NET中为If(a, b)
:
Dim value As Int32 = If(objectA?.PropertyA?.PropertyB?.PropertyC, 0) ' Default value if null