Let's say I'm working with an object of class thing
. The way I'm getting this object is a bit wordy:
BigObjectThing.Uncle.PreferredInputStream.NthRelative(5)
I'd like to see if this thing
is equal to x
or y
or z
. The naive way to write this might be:
BigObjectThing.Uncle.PreferredInputStream.NthRelative(5) == x || BigObjectThing.Uncle.PreferredInputStream.NthRelative(5) == y || BigObjectThing.Uncle.PreferredInputStream.NthRelative(5) == z
In some languages I could write something like this:
BigObjectThing.Uncle.PreferredInputStream.NthRelative(5) == x |= y |= z
but C# doesn't allow that.
Is there a C#-idiomatic way to write this test as a single expression?
Some common synonyms of complicated are complex, intricate, involved, and knotty. While all these words mean "having confusingly interrelated parts," complicated applies to what offers great difficulty in understanding, solving, or explaining.
Definition of equal (Entry 1 of 3) 1a(1) : of the same measure, quantity, amount, or number as another. (2) : identical in mathematical value or logical denotation : equivalent. b : like in quality, nature, or status. c : like for each member of a group, class, or society provide equal employment opportunities.
Just use a variable:
var relative = BigObjectThing.Uncle.PreferredInputStream.NthRelative(5); return relative == x || relative == y || relative == z;
Or if you want to get fancy with a larger set of things:
var relatives = new HashSet<thing>(new[] { x, y, z }); return relatives.Contains(BigObjectThing.Uncle.PreferredInputStream.NthRelative(5));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With