经过两年的C#,我现在回到VB.net,因为我现在的工作.在C#中,我可以在字符串变量上对null或false值进行简短的测试,如下所示:
if(!String.IsNullOrEmpty(blah))
{
...code goes here
}
Run Code Online (Sandbox Code Playgroud)
但是我对如何在VB.net中这样做有点困惑.
if Not String.IsNullOrEmpty(blah) then
...code goes here
end if
Run Code Online (Sandbox Code Playgroud)
如果字符串不为null或为空,上述语句是否意味着?Not
关键字是否像C#的!
运营商一样运作?
Roa*_*ior 13
在您显示的上下文中,VB Not
关键字确实等同于C#!
运算符.但请注意,VB Not
关键字实际上已重载以表示两个C#等效项:
!
~
例如,以下两行是等效的:
useThis &= ~doNotUse;
useThis = useThis And (Not doNotUse)
Not
就像!
(在上下文中Boolean
看到的那样.请参阅RoadWarrior的语义作为位算术中的补语).有一个特殊情况与Is
运算符结合使用来测试引用相等性:
If Not x Is Nothing Then ' … '
' is the same as '
If x IsNot Nothing Then ' … '
Run Code Online (Sandbox Code Playgroud)
相当于C#的
if (x != null) // or, rather, to be precise:
if (object.ReferenceEquals(x, null))
Run Code Online (Sandbox Code Playgroud)
这里,IsNot
优选使用.不幸的是,它不适用于TypeOf
测试.