Is there another way to write something like this:
if (a == x || a == y || a == z)
One way that I found is doing it like this:
Consider a case where a == x, and y and z are slow-to-evaluate, expensive functions.
if(a == x || a == y || a == z)
you have the benefit of the short-circuit ||
-operator, so you y and z won't be evaluated. new[] { x, y, z }
- y and z will be evaluated every time. The 'trick' with .Contains()
would be more useful if there was an elegant syntax to create lazy-evaluated sequence (IEnumerable
). i.e. something like yield return x; yield return y;...
, but inlined and shorter.