public override bool Equals(object obj)
{
var c = obj as myObj;
if (c == null) return false;
return this.Id.Equals(c.Id)
&& this.Email.Equals(c.Email)
&& this.code.Equals(c.code)
&& (this.myVal == null) ? true : (this.myVal.Equals(c.myVal))
}
This method as shown is supposed to return a boolean. When the value in “this.myVal” is null, I just want to return true (which I am doing above). Instead I get an
“Object reference not set to an instance of an object” caused in the check for this.myVal. This error indicates that the C# compiler does not care for my “null” check? Why should it complain about the null reference?
This problem is caused by the order of operations. You’re expecting this:
But what you’re actually getting is this:
Put the ternary operator inside of parentheses to ensure that it executes the way you expect it to.