Reputation: 1524
So I'm having some weird error in visual studio. The debugger crashes (I think). Here is the function where it crashes. This is for a generic BST in C#, where the == operator was overloaded to add easy comparison between nodes.
public static bool operator ==(Node<T> lhs, Node<T> rhs)
{
if ((lhs == null) || (rhs == null))
{
return false;
}
if((lhs.Data).CompareTo(rhs.Data) == 0)
{
return true;
}
else
{
return false;
}
}
It crashes at this line:
if ((lhs == null) || (rhs == null))
Upon debugging, lhs is indeed null, as soon as it starts comparing, it hangs up, then displays this message:
Then the debugging session just ends itself.
I don't really understand because in order to try to find out the problem with my code, I'm attempting to debug, but then this error happens and the session just ends itself. I've never seen this before.
Upvotes: 0
Views: 76
Reputation: 12739
You are invoking the ==
operator recursively. Use if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
to avoid this.
Alternatively, with C#7 pattern matching:
if (lhs is null || rhs is null)
Upvotes: 1