C# 比较多对布尔

C# 比较多对布尔,c#,boolean,C#,Boolean,我不确定我要进行的比较的术语: if(test1 == true && test2 == true || test2 == true && test3 == true || test3 == true && test4 == true...) { //Do stuff } 是否有有效的方法/功能来实现这一点?否则我会有一个很长的if语句。非常感谢您的帮助。您不必指定==true部分。可以这样写 if(test1 && tes

我不确定我要进行的比较的术语:

if(test1 == true && test2 == true || test2 == true && test3 == true || test3 == true && test4 == true...)
{
   //Do stuff
}

是否有有效的方法/功能来实现这一点?否则我会有一个很长的if语句。非常感谢您的帮助。

您不必指定
==true
部分。可以这样写

if(test1 && test2 || test2 && test3 || test3 && test4...)
{
   //Do stuff
}
AB + BC = B(A+C) = B && (A || C)
如果您想简化表达式本身,我建议您研究布尔代数和

这是一个
AB+BC+CD+…
形式的表达式。您可以执行的一个缩减如下所示

if(test1 && test2 || test2 && test3 || test3 && test4...)
{
   //Do stuff
}
AB + BC = B(A+C) = B && (A || C)

A还可以用来存储所有不同的布尔值,对它们的一次迭代可以用来计算这个值。这有助于提高可读性,而性能/内存占用几乎没有变化或只是略有降低。

您不必指定
==true
部分。可以这样写

if(test1 && test2 || test2 && test3 || test3 && test4...)
{
   //Do stuff
}
AB + BC = B(A+C) = B && (A || C)
如果您想简化表达式本身,我建议您研究布尔代数和

这是一个
AB+BC+CD+…
形式的表达式。您可以执行的一个缩减如下所示

if(test1 && test2 || test2 && test3 || test3 && test4...)
{
   //Do stuff
}
AB + BC = B(A+C) = B && (A || C)
A还可以用来存储所有不同的布尔值,对它们的一次迭代可以用来计算这个值。这有助于提高可读性,而性能/内存占用几乎没有变化,或者只是略有降低。

如果(test1和test2|||…)

或者您可以将其分解为多个步骤

您是否有所有单独的变量,还是它们在一个数组/列表中 在后一种情况下,您可以在循环中迭代它们

bool result = true;

foreach (bool b  in boolArray)
{
   result = result op b;
}
如果(test1和&test2 | | |…)

或者您可以将其分解为多个步骤

您是否有所有单独的变量,还是它们在一个数组/列表中 在后一种情况下,您可以在循环中迭代它们

bool result = true;

foreach (bool b  in boolArray)
{
   result = result op b;
}

您可以简单地消除布尔比较


if((test1&&test2))
相当于
if(test1==true&&test2==true)

您可以简单地消除布尔比较


if((test1&&test2))
相当于
if(test1==true&&test2==true)

我能想到的最短的是:

if((test2 && (test1 || test3)) || (test3 && test4)) {
    //Do Stuff
}

我能想到的最短的是:

if((test2 && (test1 || test3)) || (test3 && test4)) {
    //Do Stuff
}
var tests=new[]{test1,test2,test3,test4,…};
对于(int i=0;i
var tests=new[]{test1,test2,test3,test4,…};
对于(int i=0;i
如果你不介意的话,把你的书放在一个列表中,由linq使用

e、 g

bool test1=true;
bool test2=true;
bool test3=真;
bool test4=真;
List booList=新列表{test1,test2,test3,test4};
bool isTrue=booList.All(b=>b==true)//这将成为现实
bool test5=false;
booList.Add(test5);
bool isFalse=booList.All(b=>b==true)//这将返回false

PS:如果你不介意把你的bool列在一个列表中,并由linq使用它,我不知道与if语句相比,性能会如何

char test[x]

... test[x] init ...

i=0
res=0

while( i < x-2 )
{
    res |= test[i] && test[i+1]
}
e、 g

bool test1=true;
bool test2=true;
bool test3=真;
bool test4=真;
List booList=新列表{test1,test2,test3,test4};
bool isTrue=booList.All(b=>b==true)//这将成为现实
bool test5=false;
booList.Add(test5);
bool isFalse=booList.All(b=>b==true)//这将返回false
PS:我不知道与if语句相比,性能会如何
char test[x]

... test[x] init ...

i=0
res=0

while( i < x-2 )
{
    res |= test[i] && test[i+1]
}
... 测试[x]初始化。。。 i=0 res=0 而(i
字符测试[x]
... 测试[x]初始化。。。
i=0
res=0
而(i
使用C#时,可以用逻辑处理布尔值。:)

如果是bool1,那么买一些冰淇淋

如果bool1不存在,那么就不要买冰淇淋

将值与0进行比较时,可以使用not运算符(!)

如果(!bool1)MessageBox.Show(“没有冰淇淋伴侣”)

与0进行比较时也是如此,只是不要应用not运算符(!)

if(bool1)MessageBox.Show(“冰淇淋:D”)

对不起,我把它弄糊涂了

因此,为了增加其他人的职位,以下内容是合适的

if(bool1&&bool2 | | bool1&&bool3)MessageBox.Show(“冰淇淋!”)

使用C#时,可以用逻辑处理布尔值。:)

如果是bool1,那么买一些冰淇淋

如果bool1不存在,那么就不要买冰淇淋

将值与0进行比较时,可以使用not运算符(!)

如果(!bool1)MessageBox.Show(“没有冰淇淋伴侣”)

与0进行比较时也是如此,只是不要应用not运算符(!)

if(bool1)MessageBox.Show(“冰淇淋:D”)

对不起,我把它弄糊涂了

因此,为了增加其他人的职位,以下内容是合适的

if(bool1&&bool2 | | bool1&&bool3)MessageBox.Show(“冰淇淋!”)

LINQ方式(假设值位于数组中):

LINQ方式(假设值位于数组中):


或者,如果你能把bool放在一个列表中,你会翻译成LINQ.ANY

List<bool> booList = new List<bool> { true, true, true, true };
            bool isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
            booList = new List<bool> { true, true, false, false };
            isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
            booList = new List<bool> { false, false, false, false };
            isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
List booList=新列表{true,true,true};
bool isTrue=booList.Any(b=>b);
Console.WriteLine(isTrue.ToString());
booList=新列表{true,true,false,false};
isTrue=booList.Any(b=>b);
Console.WriteLine(isTrue.ToString());
booList=新列表{false,false,false};
isTrue=booList.Any(b=>b);
Console.WriteLine(isTrue.ToString());

或者如果您可以将bool放入列表中,则将转换为LINQ.ANY

List<bool> booList = new List<bool> { true, true, true, true };
            bool isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
            booList = new List<bool> { true, true, false, false };
            isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
            booList = new List<bool> { false, false, false, false };
            isTrue = booList.Any(b => b);
            Console.WriteLine(isTrue.ToString());
List booList=新列表{true,true,true};
bool isTrue=booList.Any(b=>b);
Console.WriteLine(isTrue.ToString());
booList=新列表{true,true,false,