C# Multiple conditions

To put it simply I would like to know if there is a way I can have multiple conditions to trigger a if statement.

To add to that the program I am working on will be for toner cartridge inventory management, and I want it to send an e-mail if our reserves of a particular colour is low. The code to send a e-mail is quite long to I want to avoid doing it in 4 different statements, such as:

if (k toner is low)
{
send mail
}

else if (C toner is low)
{
send mail
}

else if (M toner is low)
{
send mail
}

else if (Y toner is low)
{
send mail
}

so what I basically want is:

if (K or C or Y or M toner is low)
{
Send mail
}

Any ideas on the best way to do this?

Jon Skeet
people
quotationmark

Yup, you're just looking for the || (conditional-OR) operator:

if (kTonerIsLow || cTonerIsLow || ...) 
{
    SendEmail();
}

Note that the operator is short-circuiting - so if kTonerIsLow is true, it won't evaluate cTonerIsLow, etc. If you use | instead, it will still work - but it will evaluate both operands unconditionally.

Additionally, this comment struck me:

The code to send a e-mail is quite long to I want to avoid doing it in 4 different statements

That suggests you should probably pull it out into a separate method anyway.

people

See more on this question at Stackoverflow