最好的方式来检查多个布尔条件,在C#中的if语句 [英] Best way to check multiple boolean conditions in C# if statements

查看:488
本文介绍了最好的方式来检查多个布尔条件,在C#中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的code 3布尔(C#),并取决于是什么布尔真正 INT32 属性$ C>和
最新最好的方式来完成这项以另一种方式比if语句,如:

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:

if(a && b && !c)
   d = 1;
if(a && !b && !c)
   d = 2;
//etc.. ect...

编辑:3布尔值必须让每一个组合,可以设置INT32值

The 3 booleans must have every combination possible to set the int32 value.

编辑2:的D的值可以是相同的两个不同的布尔comparations

EDIT 2: The value of "d" can be the same for two different boolean comparations.

推荐答案

这是更好地捕捉操作的意图,而不是明确检查布尔值。

It is better to capture the intent of the operation instead of explicitly check the boolean values.

例如:

public void Check()
{
   if (HasOrdered())
   {
      // do logic
   }
}

private bool HasOrdered()
{
    return a && !b && !c;
}

private bool HasBooked()
{
    return a && b && !c;
}

这篇关于最好的方式来检查多个布尔条件,在C#中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆