在VB.NET中如何检查字典为空?

时间:2013-11-11 09:45:56

标签: vb.net dictionary

对vb.net不熟悉并在vb.net中做一个工作流程,我需要检查字典是否为空。我已经声明了一个字典,但没有给它分配任何值。

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

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

1 个答案:

答案 0 :(得分:5)

使用NothingNot Is Nothing或通过Visual Basic中的旧IsNothing函数检查IsNot Nothing的变量。

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

    If Not dict Is Nothing Then
      ' not nothing 
    End If
    
  2. IsNot Nothing

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

    If Not IsNothing(dict) Then
      ' not nothing 
    End If
    
  4. 我不再使用.NET中的VB6函数IsNothing,因为它引入了不需要的依赖项,并且提到here的原因(它允许值类型并始终返回False)。