See the following code:
Private Function EqualsNothing(ByVal item As Object) As Boolean
Return item.Equals(Nothing)
End Function
Console.WriteLine(0.Equals(Nothing)) ' True
Console.WriteLine(EqualsNothing(0)) ' False
How do I avoid Equals from returning something different after boxing structures? Is there some way to call the original Equals implementation?
I know I can use the = operator in this case, but EqualsNothing is supposed to work with both classes and structures. The = operator will not work with classes in VB.NET, and it will also not work with structures that not have implemented this operator. Equals does work with everything, but as I demonstrated above, Equals doesn’t return the same thing after boxing.
So, how should I rewrite EqualsNothing so that it works with classes and structures?
Edit: I tried making EqualsNothing generic, but that did not help.
I’m not sure this will work in all cases, but you might try this generic method: