简体   繁体   中英

How to use multiple AND operators?

I have 6x List<int> : PL1 , PL2 , PL3 , PL4 , PL5 and PL6 and I want to call a function IF ALL of those Lists don't have a count of 6.

Example:

All lists except PL4 have a count of 6 -> The function will execute.

All lists have a count of 6 -> The function will not execute.

I'm trying to achieve this with:

if (PL6.Count != 6 && PL5.Count != 6 && PL4.Count != 6 && PL3.Count != 6 && PL2.Count != 6 && PL1.Count != 6)
{
     Function();
}

.. which is not working. How do I get it to work? I tried & , && , | and || in the statement.

It sounds like the behaviour you're trying to capture is "execute the function if any of the lists don't have a count of 6". If this is your intent then you'll want the OR operator ( || ):

if (PL6.Count != 6 || PL5.Count != 6 || PL4.Count != 6 || PL3.Count != 6 || PL2.Count != 6 || PL1.Count != 6)
{
     Function();
}

Alternatively you could write this as:

if (!(PL6.Count == 6 && PL5.Count == 6 && PL4.Count == 6 && PL3.Count == 6 && PL2.Count == 6 && PL1.Count == 6))
{
     Function();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM