I’m normally at home in C#, and I’m looking at a performance issue in some VB.NET code — I want to be able to compare something to the default value for a type (kind of like C#’s default keyword).
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(?)
{
// ...
}
}
}
I was led to believe that Nothing was semantically the same, yet if I do:
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
Then when T2 is Integer, and the value of id is 0, the condition still passes, and the body of the if is evaluated. However if I do:
Public Function Bar( id As Integer ) As T1
If id <> Nothing Then
' ...
End If
End Function
Then the condition is not met, and the body is not evaluated…
This is not a complete solution, as your original C# code doesn’t compile. You can use Nothing via a local variable:
That doesn’t compile, in the same way that the C# version doesn’t compile – you can’t compare values of unconstrained type parameters like that.
You can use
EqualityComparer(Of T)though – and then you don’t even need the local variable: