I have a user object and I have a variable named userID.
I would like to test 2 things:
- user == null
- user.UserID != userID
The second test must be done only if user is not null otherwise I have a runtime error.
I wonder if it is possible to test both on the same line, something like below:
return ((user == null) && (user.UserID != userID))
But coded like above doesn’t work because when user is null I have a runtime error.
Thanks.
I assume C# and that you want a boolean value == true when the user is null OR userid is different from user.UserID. So the answer is:
The OR logical operator stop its evaluation of the expression as soon as it finds a true condition starting from the leftmost one.
In this case if user is null, you have your result (True) and there is no need to test the second condition, therefore you avoid the null reference exception.