是否有一个类似于C#的VB.net!运营商?

Jac*_*ack 4 vb.net

经过两年的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#等效项:

  • 逻辑否定: !
  • 按位补码: ~

例如,以下两行是等效的:

  • C#: useThis &= ~doNotUse;
  • VB: useThis = useThis And (Not doNotUse)


Rub*_*ben 10

是的他们是一样的

  • 他们的行为方式相同,但他们肯定不一样.VB中的"不"被重载以表示逻辑否定和按位补码. (7认同)

Kon*_*lph 6

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测试.