If I compile the following code snippet with Visual C# 2010 I ALWAYS get false:
object o = null;
Console.WriteLine("Is null: " + o == null); // returns false
Does anybody know why???
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why is easy; think of what you’ve written as actually being this:
It’s testing
"Is null: " + oagainstnull, which will always befalse. This is due to the rules of operator precedence, where+comes before==.You should explicitly apply parens to make sure it’s working like you want:
As noted in the comments by Jim Rhodes:
I noted myself that I agree; that I don’t even try to remember operator precedence rules myself, instead being explicit with parens all the time. I further suggest that this is also one reason to be very careful when relying on implicit type conversion and/or methods with multiple overloads.
I’d also like to point out that I really like something Ravadre noted in their answer; about why only “False” was printed, and not the whole text you were trying to print.