Surprisingly the code bellow will not succeed.
int? n1 = null; int? n2 = null; Assert.IsTrue(n1 <= n2); // Fails here
Do you 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.
Using boolean logic with null nullable values in C# (and VB.Net) often times defies logic. I find the best way to make sense of it is to remember that ‘null is not a value’. Because null is not a value you cannot do any operations on it. Hence things like ‘
1 > null‘ and ‘1 < null‘ are both true.Here is a detailed guide: http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx
If you do want to treat null as a value then you could use the GetValueOrDefaultMethod() to equate null with the default value. For example
This is a bit verbose but it will get the job done.