【问题标题】:How to check dictionary is null in VB.NET?如何在 VB.NET 中检查字典是否为空?
【发布时间】:2013-11-23 01:39:39
【问题描述】:

我是 vb.net 的新手并在 vb.net 中进行工作流程,我需要检查字典是否为空。我已经声明了一个字典,但没有给它赋值。

当我使用IsNothing() 方法时,它会给出对象引用异常。怎么查?

Dim CustDicAs New Dictionary(Of String, Integer)
CustDic.IsNothing()

【问题讨论】:

标签: vb.net dictionary


【解决方案1】:

您可以使用 Not Is NothingIsNot Nothing 或通过 Visual Basic 中的旧 IsNothing 函数检查 Nothing 的变量。

Dim dict As Dictionary(Of String, String)
  1. 不是什么都不是

    If Not dict Is Nothing Then
      ' not nothing 
    End If
    
  2. 不是没有

    If dict IsNot Nothing Then
      ' not nothing 
    End If
    
  3. IsNothing function(VB)

    If Not IsNothing(dict) Then
      ' not nothing 
    End If
    

我不会再在 .NET 中使用 VB6 函数 IsNothing,因为它引入了不必要的依赖关系,并且出于上述原因 here(它允许值类型并始终返回 False)。

【讨论】:

  • If Not 不会出现在 Workflow Foundation dotnet 4.5 中
  • @Sajeetharan: Not 是逻辑否定运算符,它从 VB.NET 1.0 开始可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 2022-07-21
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多