-3

If I use the following code,

if (obj.ContactDetail != null && obj.ContactDetail.Id != Guid.Empty)

Will I get a null reference error on the second half (.Id) or will it behave like javascript and fall out of the If on the first half of the comparison.

I KNOW this has been answered, but i don't know the terms to use to look it up. Sry.

CarComp
  • 1,929
  • 1
  • 21
  • 47
  • 5
    Not only is it answered [in the documentation](https://msdn.microsoft.com/en-us/library/2a723cdk.aspx), you could also have tried it. – Charles Mager Jul 13 '15 at 15:46
  • Why when i forget how something simple works, i come here, get the answer, and get rammed into the ground? BTW, I mentioned i didn't know what the term was for it. – CarComp Jul 13 '15 at 15:47
  • Its not that you are getting rammed into the ground, the code to try it out in a simple console application is far less than the effort it takes to write out the question here... Its literally 4 lines of code to test the hypothesis. – Ron Beyer Jul 13 '15 at 15:52
  • As you were told, it is a short-circuit. Every condition returns a Boolean value. In case of &&, both conditions have to be true for the entire expression be true. So if the 1st is false, the 2nd is not checked. In case of || (or), only one have to be true for the entire expression become true. If the 1st is true there's no need to check the 2nd one, the entire expression will be true. –  Jul 13 '15 at 15:53
  • 1
    @CarComp Why, because it's extremely rude and disrespectful and in violation of the rules and guidelines of the site. You've failed to do even the most basic of research or homework on your own part to try to solve the problem, or find an existing solution, and instead wasted a ton of not only your own time, but the time of many other people. Your comment also indicates that *you know what you did was wrong*, and yet you *did it anyway*, which makes it all the more rude and inappropriate. – Servy Jul 13 '15 at 15:58
  • @Servy: to be honest, your comment is more rude than the question. OP has told that he didn't know the term so he couldn't search it. However, you are right that he could have tested it. – Tim Schmelter Jul 13 '15 at 16:03
  • @TimSchmelter He specifically asked why his low quality question was received poorly, so I answered his question. Not only could he have trivially tested it, as you yourself have said, but he could have simply have looked up the documentation of the operator in question. He need not have known that the behavior he was interested in was called short circuiting to search on that. – Servy Jul 13 '15 at 16:06
  • @Servy I'll be sure to think twice before asking something. Thanks for the guidance. – CarComp Jul 13 '15 at 16:25

2 Answers2

0

That's called short-circuit evaluation and yes, && is an operator that only evaluates the second condition if the first was not already false.

MSDN:

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

The operation x && y corresponds to the operation x & y except that if x is false, y is not evaluated, because the result of the AND operation is false no matter what the value of y is. This is known as "short-circuit" evaluation. The conditional-AND operator cannot be overloaded, but overloads of the regular logical operators and operators true and false are, with certain restrictions, also considered overloads of the conditional logical operators.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you for the time you offered me. I appreciate it. – CarComp Jul 13 '15 at 15:48
  • 1
    @CarComp: i don't know if you are familiar with VB.NET but even if it is too verbose the VB operator is more readable since it is `AndAlso`. You can translate `&&` with `AndAlso` to remember it. You only need to check "also" a second condition if the first was not satisfactory. – Tim Schmelter Jul 13 '15 at 15:51
0

NO, you will not get a NRE cause if the first part obj.ContactDetail != null evaluates to false then it will not evaluate the second part at all. Second part will be evaluated only when the first part evaluates to TRUE.

Rahul
  • 76,197
  • 13
  • 71
  • 125