Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want a short way in logical conditions while controlling if more than one variables are greater or lower than a constant number. Such as for a Rectangle rect;

if(rect.Left > 10 | rect.Top > 10 | rect.Bottom > 10 | rect.Right > 10)

this logical condition can be more easy with this use of "|";

if(rect.Left | rect.Top | rect.Bottom | rect.Right > 10)

This means if left or top or bottom or right greater than 10 return true.

I hope I am clear in my question.
Posted
Updated 11-Sep-15 7:48am
Comments
Philippe Mori 10-Sep-15 23:47pm    
By the way, logical or operator is || anot |. The first one will only evaluate conditions until the answer is known and also have a more intuitive operator precedence. You should uses | only for binary or (bit per bit OR).
Sergey Alexandrovich Kryukov 11-Sep-15 1:00am    
Not exactly. Logical operators are || or |. The second one is for full evaluation without shortcut. So, code with | is not optimal, but correct, at least when side effect don't make the difference.
And yes, binary is |.
—SA

Nope, that shorthand doesn't exist in the language.
However, you can approximate it with Linq:
C#
if(new int[]{ rect.Left, rect.Top, rect.Bottom, rect.Right }.Any(v => v > 10))

This does cause the allocation of an array, so if you are going to do stuff like this a lot, then make a method for each type of operand.
 
Share this answer
 
Comments
Amt-Coder 10-Sep-15 19:13pm    
This type of use is not short but acceptable. Thanks Matt.
Dave Kreskowiak 10-Sep-15 23:20pm    
There is no other shortcut available to do this. Come to think of it, I don't know of a single language that allows this the way you want to implement it.
Sergey Alexandrovich Kryukov 11-Sep-15 1:01am    
5ed.
—SA
Operator | between integer values will be treated as binary OR. That's it.
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900