在 Python 中,我可以做这样的事情,我认为这比一个语句中的两对条件更清楚:
if 1 < 2 < 3:
print 'triple testing?'
else:
print 'more like triple failing!'
没有遇到任何问题,但在 C# 中,它将第一个比较转换1 < 2
为 a bool
,然后再继续,所以它抛出了一个异常Operator '<' cannot be applied to operands of type 'bool' and 'int'
。
有没有办法避免添加第二个条件,所以我不必像在 Python 中那样将三重条件分成两组,而不是一个“三重”条件?
if (1 < 2 & 2 < 3) { ... }
编辑:我使用它的一个例子是确保 anint
在一系列数字之间。
if ((0 < x) && (x < 10) { ... } //I can already do this
vs
if (0 < x < 10) { ... } //Would rather have something like this