C# IF 语句多个条件,同一个语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1341513/
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
IF Statement multiple conditions, same statement
提问by markdigi
Hey all, looking to reduce the code on my c# if statements as there are several repeating factors and was wondering if a trimmer solution is possible.
大家好,希望减少我的 c# if 语句上的代码,因为有几个重复因素,并且想知道是否可以使用修剪器解决方案。
I currently have 2 if statements that need to carry out an identical statement, however the only variable is an extra condition on an if statement when a checkbox is not checked. Im just wondering if there is a way to make it one statement or make the condition string variable, heres the compressed version of the code:
我目前有 2 个 if 语句需要执行相同的语句,但是唯一的变量是当未选中复选框时 if 语句上的额外条件。我只是想知道是否有办法使它成为一个语句或使条件字符串变量,这是代码的压缩版本:
if (checkbox.checked)
{
if (columnname != a && columnname != b && columnname != c)
{
"statement 1"
}
}
else
{
if (columnname != a && columnname != b && columnname != c
&& columnname != A2)
{
"statement 1"
}
}
Its like I need to run an if statement within the conditions of an if statement if that makes sense, like this psuedo form:
就像我需要在 if 语句的条件内运行 if 语句一样,如果有意义的话,就像这个伪形式:
if (columnname != a
&& columnname != b
&& columnname != c
&& if(checkbox.checked{columnname != A2})
采纳答案by Daniel Elliott
if (columnname != a
&& columnname != b
&& columnname != c
&& (checkbox.checked || columnname != A2))
{
"statement 1"
}
Should do the trick.
应该做的伎俩。
回答by mikefrey
if (columnname != a && columnname != b && columnname != c
&& (columnname != A2 || checkbox.checked))
{
"statement 1"
}
回答by gjutras
if (checkbox.checked && columnname != a && columnname != b && columnname != c)
{
"statement 1"
}
else if (columnname != a && columnname != b && columnname != c
&& columnname != A2)
{
"statement 1"
}
is one way to simplify a little.
是一种简化一点的方法。
回答by JBrooks
Isn't this the same:
这不是一样的吗:
if ((checkbox.checked || columnname != A2) &&
columnname != a && columnname != b && columnname != c)
{
"statement 1"
}
回答by Meta-Knight
You could also do this if you think it's more clear:
如果您认为更清楚,您也可以这样做:
if (columnname != a
&& columnname != b
&& columnname != c
{
if (checkbox.checked || columnname != A2)
{
"statement 1"
}
}
回答by Jason Hymanson
I think agileguy has the correct answer, but I would like to add that for more difficult situations there are a couple of strategies I take to solve the problem. The first is to use a truth table. If you Google "truth table" you will run across some examples related directly to programming and computer science.
我认为agileguy 有正确的答案,但我想补充一点,对于更困难的情况,我会采取几种策略来解决问题。首先是使用真值表。如果你谷歌“真值表”,你会遇到一些与编程和计算机科学直接相关的例子。
Another strategy I take is to use an anonymous function to encapsulate common logic between various conditions. Create it right before the if block, then use it where needed. This seems to create code that is more readable and maintainable.
我采取的另一个策略是使用匿名函数来封装各种条件之间的通用逻辑。在 if 块之前创建它,然后在需要的地方使用它。这似乎创建了更具可读性和可维护性的代码。
回答by markh44
I always try to factor out complex boolean expressions into meaningful variables (you could probably think of better names based on what these columns are used for):
我总是尝试将复杂的布尔表达式分解为有意义的变量(根据这些列的用途,您可能会想到更好的名称):
bool notColumnsABC = (columnname != a && columnname != b && columnname != c);
bool notColumnA2OrBoxIsChecked = ( columnname != A2 || checkbox.checked );
if ( notColumnsABC
&& notColumnA2OrBoxIsChecked )
{
"statement 1"
}
回答by Sturm
Pretty old question but check this for a more clustered way of checking conditions:
很老的问题,但请检查这个以获取更多集群检查条件的方法:
private bool IsColumn(string col, params string[] names) => names.Any(n => n == col);
usage:
用法:
private void CheckColumn()
{
if(!IsColumn(ColName, "Column A", "Column B", "Column C"))
{
//not A B C column
}
}
回答by Dan
var test = new char[] {a, b, c}.Contains(columnname));
if(test)
{
"true statement"
}
else
{
"false statement"
}
回答by BLong
How about maybe something like this?
也许像这样的事情怎么样?
var condCheck1 = new string[]{"a","b","c"};
var condCheck2 = new string[]{"a","b","c","A2"}
if(!condCheck1.Contains(columnName) && !checkbox.checked)
//statement 1
else if (!condCheck2.Contains(columnName))
//statment 2