VB equiv - 检查 属性 的 属性 是否为 null 的优雅方式
VB equiv - Elegant way to check if a property's property is null
我注意到这个很棒的 post 关于如何对嵌套 objects/properties 进行内联空值检查:C# elegant way to check if a property's property is null
是否有 VB.NET 等效项可用?
是的。空条件运算符(MSDN)也存在于VB.NET(VB 14 及以上,即Visual Studio 2015 及以上)并且具有相同的语法:
Dim value As Int32? = objectA?.PropertyA?.PropertyB?.PropertyC
通常,这与空合并运算符结合使用,在 C# 中为 a ?? b
,在 VB.NET 中为 If(a, b)
:
Dim value As Int32 = If(objectA?.PropertyA?.PropertyB?.PropertyC, 0) ' Default value if null
我注意到这个很棒的 post 关于如何对嵌套 objects/properties 进行内联空值检查:C# elegant way to check if a property's property is null
是否有 VB.NET 等效项可用?
是的。空条件运算符(MSDN)也存在于VB.NET(VB 14 及以上,即Visual Studio 2015 及以上)并且具有相同的语法:
Dim value As Int32? = objectA?.PropertyA?.PropertyB?.PropertyC
通常,这与空合并运算符结合使用,在 C# 中为 a ?? b
,在 VB.NET 中为 If(a, b)
:
Dim value As Int32 = If(objectA?.PropertyA?.PropertyB?.PropertyC, 0) ' Default value if null