Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this big complicated thing equal to this? or this? or this?

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?

like image 586
Joe Avatar asked Jun 04 '15 17:06

Joe


People also ask

What to say instead of it's complicated?

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.

What is this word equal?

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.


1 Answers

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)); 
like image 171
Jacob Avatar answered Oct 06 '22 04:10

Jacob