'Null propagation with ?? does not work as expected [duplicate]

I have estimated a courious effect with null propagation as you can see here:

1. string x = "SomeString";    
2. Console.WriteLine(! x?.Equals(null) ?? (false) ? true : false);   // true
3. Console.WriteLine(! x?.Equals(null) ?? (true) ? false : true);    // false (!)
4. Console.WriteLine(! x?.Equals(null));                             // true 

Can someone explain why the expression in Line 3. evaluates to false? As I understand the null propagation operator ?? is only executed, if an expression is null, but as you can see in line 4. the expression to the left evaluates to true, and not to null.

So if !x?.Equals(null) is true, why the null propagation to the right ?? (true) ? false : true changes this value to false?

I'm sure the error is to me and my understanding, but I have broken this error down now for hours, and I am really confused. Thanks for any explanation to this.



Solution 1:[1]

The expression on Line 4 is true.

Using that in L3: The null coalescing operator has no effect, as L4 is not null, which leaves you with:

L4 ? false : true

Which evaluates to false when L4 is true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ian