How can I compare multiple variables to a single condition?

后端 未结 7 733
时光取名叫无心
时光取名叫无心 2020-12-19 16:06

I have this:

if (input.Text.ToUpper() == \"STOP\")

But there are so many possible values that I wouldn\'t be able to specify them all sepa

7条回答
  •  -上瘾入骨i
    2020-12-19 16:20

    You can hold an array of the possible words and match it with the Contains method:

    string[] validInput = new string[] { "STOP", "END", "NO", "YES" };
    
    // input is the input you have
    if (validInput.Contains(input.Text.ToUpper()))
    {
        // Do something
    }
    

提交回复
热议问题