如何在 VB.NET 中检查字典是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19903210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check dictionary is null in VB.NET?
提问by Sajeetharan
Am new to vb.net and doing a work flow in vb.net, i need to check whether a dictionary is null. I have declared a dictionary, but have not assigned any value to it.
我是 vb.net 的新手并在 vb.net 中执行工作流程,我需要检查字典是否为空。我已经声明了一个字典,但没有为其分配任何值。
When i use IsNothing()method it gives object reference exception. How can i check?
当我使用IsNothing()方法时,它会给出对象引用异常。我该如何检查?
Dim CustDicAs New Dictionary(Of String, Integer)
CustDic.IsNothing()
回答by Tim Schmelter
You check variables for Nothingwith Not Is Nothingor IsNot Nothingor via the old IsNothingfunction from Visual Basic.
您可以Nothing使用Not Is Nothing或IsNot Nothing通过IsNothingVisual Basic 中的旧函数检查变量。
Dim dict As Dictionary(Of String, String)
Not Is Nothing
If Not dict Is Nothing Then ' not nothing End IfIsNot Nothing
If dict IsNot Nothing Then ' not nothing End IfIf Not IsNothing(dict) Then ' not nothing End If
不是什么都不是
If Not dict Is Nothing Then ' not nothing End If不是什么都没有
If dict IsNot Nothing Then ' not nothing End IfIf Not IsNothing(dict) Then ' not nothing End If
I wouldn't use the VB6 function IsNothingin .NET anymore since it introduces unneeded dependencies and for the reason mentioned here(it allows value types and always returns False).
我不会再IsNothing在 .NET 中使用 VB6 函数,因为它引入了不需要的依赖项,并且出于此处提到的原因(它允许值类型并始终返回False)。

