c# - Compare multiple values in one condition -
this question has answer here:
int32 int1, int2, int3 = 123;
given above variables, how can test of variables have value 123
without creating collection perform any
or on?
what i've tried
if(int1 == int2 == int3 == 123) { // fantastic code here }
edit
i must apologise, haven't been clear enough in question. i'm aware of &&
operator, asking regard 'elegance', i.e. how can avoid repeating value want compare against.
in same way have assigned 3 integer variables same value in 1 hit, i'd make comparison. it's looking though there no way of doing this, far. think i'm asking impossible, i'll have stick basic syntax , keep simple.
you can create usefull extension function:
public static bool equalsall<t>(this t val, params t[] values) { return values.all(v => v.equals(val)); }
call like:
bool res = 123.equalsall(int1,int2,int3);
Comments
Post a Comment