In Python I can do something like this, which I think is more clear, than two pairs of conditionals in one statement:
if 1 < 2 < 3:
print 'triple testing?'
else:
print 'more like triple failing!'
without running into any issues, but in C#, it converts the first comparison, 1 < 2 into a bool before moving on, so it’s throwing an exception Operator '<' cannot be applied to operands of type 'bool' and 'int'.
Is there a way to avoid adding the second condition, so I don’t have to split up the triple condition into two sets of pairs, rather than one ‘triple’ condition, as I can do in Python?
if (1 < 2 & 2 < 3) { ... }
edit: An example where I’d use this is to make sure an int is between a range of numbers.
if ((0 < x) && (x < 10) { ... } //I can already do this
vs
if (0 < x < 10) { ... } //Would rather have something like this
There is no way of doing
1 < 2 < 3in C#. However, you could write a quick extension method to mimic it:If is definitely overkill for 3 numbers but may be useful if there are more values to compare or they are not hard coded.