vb.net IsNothing 与 Is Nothing
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5791/
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
IsNothing versus Is Nothing
提问by Luke Girvin
Does anyone here use VB.NET and have a strong preference for or against using IsNothing
as opposed to Is Nothing
(for example, If IsNothing(anObject)
or If anObject Is Nothing...
)? If so, why?
这里有没有人使用 VB.NET 并且强烈偏爱或反对使用IsNothing
而不是Is Nothing
(例如,If IsNothing(anObject)
或If anObject Is Nothing...
)?如果是这样,为什么?
EDIT: If you think they're both equally acceptable, do you think it's best to pick one and stick with it, or is it OK to mix them?
编辑:如果您认为它们都同样可以接受,您认为最好选择一个并坚持使用,还是可以混合使用?
采纳答案by lomaxx
If you take a look at the MSIL as it's being executed you'll see that it doesn't compile down to the exact same code. When you use IsNothing() it actually makes a call to that method as opposed to just evaluating the expression.
如果您查看正在执行的 MSIL,您会发现它不会编译为完全相同的代码。当您使用 IsNothing() 时,它实际上会调用该方法,而不仅仅是评估表达式。
The reason I would tend to lean towards using "Is Nothing" is when I'm negating it becomes "IsNot Nothing' rather than "Not IsNothing(object)" which I personally feel looks more readable.
我倾向于使用“Is Nothing”的原因是,当我否定它时,它会变成“IsNot Nothing”而不是“Not IsNothing(object)”,我个人认为这看起来更具可读性。
回答by Hyman Snipes
I find that Patrick Steeleanswered this question best on his blog: Avoiding IsNothing()
我发现Patrick Steele在他的博客上回答了这个问题最好:Avoiding IsNothing()
I did not copy any of his answer here, to ensure Patrick Steele get's credit for his post. But I do think if you're trying to decide whether to use Is Nothing or IsNothing you should read his post. I think you'll agree that Is Nothing is the best choice.
我没有在这里复制他的任何回答,以确保帕特里克斯蒂尔的帖子得到认可。但我确实认为,如果你想决定是使用 Is Nothing 还是 IsNothing,你应该阅读他的帖子。我想你会同意 Is Nothing 是最好的选择。
Edit - VoteCoffe's comment here
编辑 - VoteCoffe 的评论在这里
Partial article contents: After reviewing more code I found out another reason you should avoid this: It accepts value types! Obviously, since IsNothing() is a function that accepts an 'object', you can pass anything you want to it. If it's a value type, .NET will box it up into an object and pass it to IsNothing -- which will always return false on a boxed value! The VB.NET compiler will check the "Is Nothing" style syntax and won't compile if you attempt to do an "Is Nothing" on a value type. But the IsNothing() function compiles without complaints. -PSteele – VoteCoffee
部分文章内容:在查看更多代码后,我发现了另一个应该避免这种情况的原因:它接受值类型!显然,由于 IsNothing() 是一个接受“对象”的函数,因此您可以将任何您想要的传递给它。如果它是一个值类型,.NET 会将它装箱成一个对象并将它传递给 IsNothing——它总是在装箱的值上返回 false!VB.NET 编译器将检查“Is Nothing”样式语法,如果您尝试对值类型执行“Is Nothing”,则不会编译。但是 IsNothing() 函数编译时没有抱怨。-PSteele – VoteCoffee
回答by KyleMit
You should absolutely avoid using IsNothing()
你绝对应该避免使用 IsNothing()
Here are 4 reasonsfrom the article IsNothing() VS Is Nothing
以下是文章IsNothing() VS Is Nothing中的4 个原因
Most importantly,
IsNothing(object)
has everything passed to it as an object, even value types! Since value types cannot beNothing
, it's a completely wasted check.
Take the following example:Dim i As Integer If IsNothing(i) Then ' Do something End If
This will compile and run fine, whereas this:
Dim i As Integer If i Is Nothing Then ' Do something End If
Will not compile, instead the compiler will raise the error:
'Is' operator does not accept operands of type 'Integer'.
Operands must be reference or nullable types.IsNothing(object)
is actually part of part of theMicrosoft.VisualBasic.dll
.
This is undesirable as you have an unneeded dependency on the VisualBasic library.Its slow - 33.76% slower in fact (over 1000000000 iterations)!
Perhaps personal preference, but
IsNothing()
reads like a Yoda Condition. When you look at a variable you're checking it's state, with it as the subject of your investigation.i.e. does itdo x? --- NOTIs
x
ing a property of it?So I think
If a IsNot Nothing
reads better thanIf Not IsNothing(a)
最重要的是,
IsNothing(object)
所有东西都作为对象传递给它,甚至是值类型!由于值类型不能为Nothing
,因此完全浪费了检查。
以下面的例子为例:Dim i As Integer If IsNothing(i) Then ' Do something End If
这将编译并运行良好,而这:
Dim i As Integer If i Is Nothing Then ' Do something End If
不会编译,而是编译器会引发错误:
“是”运算符不接受“整数”类型的操作数。
操作数必须是引用或可为空的类型。IsNothing(object)
实际上是一部分的一部分Microsoft.VisualBasic.dll
。
这是不可取的,因为您对 VisualBasic 库有不必要的依赖。它很慢——实际上慢了 33.76%(超过 1000000000 次迭代)!
也许个人喜好,但
IsNothing()
读起来像尤达条件。当您查看一个变量时,您正在检查它的状态,并将其作为您调查的主题。即它做 x 吗?--- NOT是它的
x
一个属性吗?所以我认为
If a IsNot Nothing
阅读比If Not IsNothing(a)
回答by proudgeekdad
I agree with "Is Nothing". As stated above, it's easy to negate with "IsNot Nothing".
我同意“什么都不是”。如上所述,很容易用“IsNot Nothing”来否定。
I find this easier to read...
我觉得这更容易阅读......
If printDialog IsNot Nothing Then
'blah
End If
than this...
比这个...
If Not obj Is Nothing Then
'blah
End If
回答by Adam Haile
VB is full of things like that trying to make it both "like english" and comfortable for people who are used to languages that use () and {} a lot. For example, on the "like english"side...VB has the "Ain't" keyword...and no, I'm not joking. And on the other side, as you already probably know most of the time you can use () with function calls if you want to, but don't have to.
VB 充满了类似的东西,试图使它既“像英语一样”又让习惯使用 () 和 {} 的语言的人感到舒服。例如,在“like english”方面……VB 有“ Ain't”关键字……不,我不是在开玩笑。另一方面,正如您可能已经知道的那样,如果您愿意,可以在大多数情况下将 () 与函数调用一起使用,但不必如此。
I prefer IsNothing()...but I use C and C#, so that's just what is comfortable. And I think it's more readable. But go with whatever feels more comfortable to you.
我更喜欢 IsNothing()...但我使用 C 和 C#,所以这很舒服。而且我认为它更具可读性。但是选择你觉得更舒服的任何东西。
回答by deadtime
回答by Brian Childress
I also tend to use the Is Nothing version partially from using it as much as I do in SQL.
我也倾向于使用 Is Nothing 版本,部分原因是我在 SQL 中使用它。
回答by Chris Tybur
I initially used IsNothing but I've been moving towards using Is Nothing in newer projects, mainly for readability. The only time I stick with IsNothing is if I'm maintaining code where that's used throughout and I want to stay consistent.
我最初使用 IsNothing,但我一直在转向在较新的项目中使用 Is Nothing,主要是为了可读性。我唯一坚持使用 IsNothing 的情况是,如果我要维护在整个过程中使用的代码并且我想保持一致。
回答by ybacou
Is Nothing requires an object that has been assigned to the value Nothing. IsNothing() can take any variable that has not been initialized, including of numeric type. This is useful for example when testing if an optional parameter has been passed.
Is Nothing 需要一个已分配给 Nothing 值的对象。IsNothing() 可以采用任何尚未初始化的变量,包括数字类型。例如,在测试是否传递了可选参数时,这很有用。