C#”;或“;操作人员

本文关键字:操作 | 更新日期: 2023-09-27 18:24:58

我有这个代码:

if (textBox1.Text == "one" || "two")

我曾尝试使用||和|来添加更多的字符串,但它表示不能应用于"bool"answers"string"类型的操作数。我怎样才能做到这一点?非常感谢。

C#”;或“;操作人员

试试这个

if (textBox1.Text == "one" || textBox1.Text == "two")

或者:

var strings = new List<string>() {"one", "two", "thee", .... "n"};
if(strings.Contains(textBox1.Text)){
}

你不能像我怀疑的那样组合运算符:

if (textBox1.Text == "one" || "two")

您需要对每个条件进行以下限定:

if (textBox1.Text == "one" || textBox1.Text == "two")

有一些方法可以更容易地做到这一点,请参阅此问题的答案以获得另一种方法

我建议使用:

var options = new [] { "one", "two" };
if (options.Contain(textBox1.Text))
    ...