The following would work in C/C++ because it has no first class support for booleans, it treats every expression with "on" bit on them as true otherwise false. In fact, the following code would not work in C# or Java if x and y are of numeric types.
if (x | y)
So the explicit version of above code is:
if ( (x | y) != 0)
In C, any expression which would have an "On" bit on them, results to true
int i = 8;
if (i) // valid in C, results to true
int joy = -10;
if (joy) // vaild in C, results to true
Now, back to C#
If x and y are of numeric type, your code: if (x | y) will not work. Have you tried compiling it? It will not work
But for your code, which I could assume x and y are of boolean types, so it will work, so the difference between | and || for boolean types, the || is short-circuited, the | is not. The output of the following:
static void Main()
{
if (x | y)
Console.WriteLine("Get");
Console.WriteLine("Yes");
if (x || y)
Console.WriteLine("Back");
Console.ReadLine();
}
static bool x
{
get { Console.Write("Hey"); return true; }
}
static bool y
{
get { Console.Write("Jude"); return false; }
}
is:
HeyJudeGet
Yes
HeyBack
Jude won't be printed twice, the || is a boolean operator, many C-derived languages boolean operators are short-circuited, boolean expressions are more performant if they are short-circuited.
As for layman terms, when you say short-circuited, for example in || (or operator), if the first expression is already true, no need to evaluate the second expression. Example: if (answer == 'y' || answer == 'Y'), if the user press small y, the program don't need to evaluate the second expression(answer == 'Y'). That's short-circuit.
On my sample code above, X is true, so the Y on || operator won't be evaluated further, hence no second "Jude" output.
Don't use this kind of code in C# even if X and Y are of boolean types: if (x | y). Not performant.