我通常使用C#编程,现在需要调查一下VB.NET代码的性能问题——我想要比较某个变量与其类型的默认值(有点像C#中的default
关键字)。
public class GenericThing<T1, T2>
{
public T1 Foo( T2 id )
{
if( id != default(T2) ) // There doesn't appear to be an equivalent in VB.NET for this(?)
{
// ...
}
}
}
我曾相信Nothing
与语义相同,但如果我执行以下操作:
Public Class GenericThing(Of T1, T2)
Public Function Foo( id As T2 ) As T1
If id IsNot Nothing Then
' ...
End If
End Function
End Class
当T2
是Integer
类型,且id
的值为0
时,条件仍然满足,if
语句的内容将被执行。但是,如果我这样做:
Public Function Bar( id As Integer ) As T1
If id <> Nothing Then
' ...
End If
End Function
那么条件不满足,就不会执行代码块...
Nothing
... - Rowland Shaw