在 C# if 语句中检查多个布尔条件的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15905017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to check multiple boolean conditions in C# if statements
提问by Phoenix_uy
I have 3 booleans on my code (C#) and an int32
property that depends on what booleans are true
and false
.
Whats the best way to accomplish this in another way than if statements like:
我的代码 (C#) 上有 3 个布尔值和一个int32
取决于布尔值是什么的属性true
和false
. 以另一种方式完成此操作的最佳方法是什么,而不是像这样的 if 语句:
if(a && b && !c)
d = 1;
if(a && !b && !c)
d = 2;
//etc.. ect...
EDIT: The 3 booleans must have every combination possible to set the int32 value.
编辑:3 个布尔值必须具有所有可能的组合来设置 int32 值。
EDIT 2: The value of "d" can be the same for two different boolean comparations.
编辑 2:对于两个不同的布尔比较,“d”的值可以相同。
采纳答案by Davin Tryon
It is better to capture the intent of the operation instead of explicitly check the boolean values.
最好捕获操作的意图,而不是显式检查布尔值。
For example:
例如:
public void Check()
{
if (HasOrdered())
{
// do logic
}
}
private bool HasOrdered()
{
return a && !b && !c;
}
private bool HasBooked()
{
return a && b && !c;
}
回答by sehe
You could do the lookup table hint given by @Adriano, assuming you have lookup_table
filled with values for index [0..8):
您可以执行@Adriano 给出的查找表提示,假设您已lookup_table
填充索引 [0..8) 的值:
var index = new [] { a,b,c }.Aggregate(0, (a,i) => return 2*a + (i?1:0));
int d = lookup_table[index];
EditThe EDIT of the question made this irrelevant: What does d mean?
编辑问题的编辑使这无关紧要:d 是什么意思?
If it's the count of false values (possible from the sample code), make it
int d = new [] { a,b,c }.Count(b => !b);
如果它是错误值的计数(可能来自示例代码),请将其设为
int d = new [] { a,b,c }.Count(b => !b);
回答by PsyKzz
I think what your doing now is perfectly fine and any other solutions would be down to preference.
我认为你现在所做的一切都很好,任何其他解决方案都取决于偏好。
My preference, where it applies would be to separate the checks out if possible.
我的偏好是,如果可能的话,它适用的地方是将支票分开。
if (!a)
return;
if (!b)
return;
if (!c)
return;
This would be useful in the event that you need to check certain prereqs before issuing a function, like if the user has logged in, if a parameter exists and is in the right context, along with other items.
如果您需要在发出函数之前检查某些先决条件,例如用户是否已登录,参数是否存在且在正确的上下文中,以及其他项目,这将非常有用。
Like i said this might not apply but i just wanted to voice my opinion
就像我说的这可能不适用但我只是想表达我的意见
回答by D Stanley
I don't see anything wrongwith how you're doing it, but if the output is the same for multiple conditions you maybe able to simplify if by creating a truth table and simplifying the conditions.
我认为您的操作方式没有任何问题,但是如果多个条件的输出相同,您可以通过创建真值表并简化条件来简化。
For example, if d
should be 0
anytimea
is false you could simplify to:
例如,如果d
应该0
随时a
为假,您可以简化为:
if(a)
if(b && !c)
d = 1;
if(!b && !c)
d = 2;
...
else
d = 0;
Or if there is some mathematical pattern (e.g. a
, b
, and c
represent the three digits of a binary number) then you could do bit arithmetic.
或者,如果有一些数学模式(例如a
,b
, 和c
代表二进制数的三位数字),那么您可以进行位算术。
If, however, you have 8 distinctoutcomes (one for each combination of a
, b
, and c
) then your method is fine.
但是,如果你有8种不同的结局(一个用于的每个组合a
,b
和c
),那么你的方法是好的。
回答by SamFisher83
You could use a Karnaugh map to reduce your equations and have fewer ifs.
您可以使用卡诺图来减少方程并减少 if。