Is it possible somehow in C #: if (a == b == c == d) {...}

Is there a quick way to compare the equality of more than one value in C #?

something like:

if (5==6==2==2){

//do something

}

thank

+5
source share
5 answers
public static class Common {
    public static bool AllAreEqual<T>(params T[] args)
    {
        if (args != null && args.Length > 1)
        {
            for (int i = 1; i < args.Length; i++)
            {
                if (args[i] != args[i - 1]) return false;
            }
        }

        return true;
    } 
}

...

if (Common.AllAreEqual<int>(a, b, c, d, e, f, g)) 

This can help:)

+14
source
if (a == b && b == c && c == d) {
    // do something
}
+28
source

# (==) bool, 5 == 6 false.

5 == 6 == 2 == 2

(((5 == 6) == 2) == 2)

((false == 2) == 2)

a bool int. , , , , , .

- , @Joachim Sauer:

 a == b && b == c && c == d
+13
source

No, this is not possible; you need to break it down into separate statements.

if(x == y && x == z) // now y == z
{
}

Good luck.

+5
source

There is no direct way to do this with C #, but you can go with some helper class. Check out this VB.NET section on this VB.Net issue : check multiple values ​​for equality?

+1
source

All Articles