-3
    while (currentNode?.BinaryComp(_value) != null);


    public static bool operator !=(Node<T> f1, Node<T> f2)
    {
        return f1.Value.CompareTo(f2.Value) != 0;
    }

System.NullReferenceException:

Osendo
  • 15
  • 2
  • BinaryComp() returns null – Osendo Apr 10 '19 at 05:10
  • 4
    It would be awesome if you could provide a [mcve]. It may also be worthwhile explaining why you have a while loop that doesn't seem to _do_ anything. – mjwills Apr 10 '19 at 05:17
  • 2
    I think there's a misunderstanding what OP is asking here. As far as I can tell, OP isn't asking why they are getting a `NullReferenceException`, or how they can prevent it, they seem to be asking why it uses their operator overload and not the default `object` equality check. Is that correct, Osendo? – ProgrammingLlama Apr 10 '19 at 05:26
  • 4
    If i could vote to close this, i would, its completely and entirely unclear what you are asking. What you are expecting to happen or why – TheGeneral Apr 10 '19 at 05:29
  • Not an answer, but every time I see `? true : false` I wonder why people bother to even write it. It is absolutely useless. – oerkelens Apr 10 '19 at 06:08
  • I asking why it uses operator overload and not the default object equality check. – Osendo Apr 10 '19 at 06:57

1 Answers1

1

You can use ReferenceEquals. Also, if f1 or f2 is null, then f1.Value will throw an exception. Either use, f1?.Value or use referenceequals.

Your code should look something like this:

public static bool operator !=(Node<T> f1, Node<T> f2)
{
    if (object.ReferenceEquals(f1, null))
    {
         return object.ReferenceEquals(f2, null);
    }

    return f1.Value.CompareTo(f2.Value);
}

Here is an interesting article on Equals, ==, ReferenceEquals

C# .Equals(), .ReferenceEquals() and == operator

Gauravsa
  • 6,330
  • 2
  • 21
  • 30