Alex Rivera | Logout

A clear, layman's explanation of the difference between | and || in c#?

Asked 2009-03-26T05:43:56.090
44

Ok, so I've read about this a number of times, but I'm yet to hear a clear, easy to understand (and memorable) way to learn the difference between:

if (x | y)

and

if (x || y)

..within the context of C#. Can anyone please help me learn this basic truth, and how C# specifically, treats them differently (because they seem to do the same thing). If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise?

Edit
Report

1 Answer

2

Without delving into the details in any way, shape, or form, here's a real layman's version.

Think of "|" as a straight "or" in english; think of "||" as "or else" in english.

Similarly think of "&" as "and" in english; think of "&&" as "and also" in english.

If you read an expression to yourself using these terms, they often make a lot more sense.

answered 2009-03-26T12:10:13.603

Your Answer