有没有办法使用垂直条 - | - 变量之间? [英] Is there any way to use vertical bar - | - between variables?

查看:54
本文介绍了有没有办法使用垂直条 - | - 变量之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在逻辑条件下有一个简短的方法,同时控制多个变量是大于还是小于常数。例如对于矩形矩形;



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



使用|这个逻辑条件可以更容易;



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



这意味着如果左边或顶部,底部或右边大于10返回true。



我希望我的问题清楚。

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.

推荐答案

不,那个简写没有不存在于语言中。

但是,您可以使用Linq来估算它:

Nope, that shorthand doesn't exist in the language.
However, you can approximate it with Linq:
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.


Operator |整数值之间将被视为二进制OR。就是这样。

-SA
Operator | between integer values will be treated as binary OR. That's it.
—SA


这篇关于有没有办法使用垂直条 - | - 变量之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆