This is normally something I can find online pretty easily but I think I'm having trouble wording it so I apologize if this is a duplicate question.
I'm looking for a more concise way to do an IF/OR check for the same query. For example:
if (sCheck == "string1" || sCheck == "string2" || sCheck == "string3")
{
MessageBox.Show(sCheck + " is one of the three possible strings.");
}
I'm looking for a cleaner more concise way to do the same If/Or. I was hoping something like these would work but of course they don't:
if (sCheck == "string1" || "string2" || "string3") { }
if (sCheck == ("string1" || "string2" || "string3")) { }
Create a collection that holds the different possibilities:
if(new[] {"string1", "string2", "string3" }.Contains(sCheck)) { }
You can create a collection of string
and then use the Contains
method:
List<string> myStrings = new List<string>(){"string1", "string2" , "string3"};
if (myStrings.Contains(sCheck))
{
//Do Work
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With