【问题标题】:VB equiv - Elegant way to check if a property's property is nullVB equiv - 检查属性的属性是否为空的优雅方法
【发布时间】:2017-05-11 16:39:53
【问题描述】:

我注意到这篇关于如何对嵌套对象/属性进行内联空值检查的精彩帖子:C# elegant way to check if a property's property is null

是否有可用的 VB.NET 等价物?

【问题讨论】:

标签: vb.net equivalent null-check


【解决方案1】:

是的。空条件运算符 (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

【讨论】:

    猜你喜欢
    • 2011-03-28
    • 1970-01-01
    • 2014-07-11
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2014-03-05
    相关资源
    最近更新 更多