我可以在c#中扩展条件吗?

时间:2016-12-01 19:38:01

标签: c# if-statement

如果选中复选框,我想扩展条件吗?

string Condition= "A==B"  
if (chechbox1.Checked==true)  
{  
    Condition+="&& B==C";  
}  
if (chechbox2.Checked==true)  
{  
    Condition+="&& C==D";  
}  
if (Condition)  
{  
    //do something  
}  

2 个答案:

答案 0 :(得分:2)

使用布尔逻辑:

bool Condition = A == B;  
if (chechbox1.Checked)  
{  
    Condition &= B == C;  
}  
if (chechbox2.Checked)  
{  
    Condition &= C == D;  
}  

if (Condition)  
{  
    //do something  
}  

答案 1 :(得分:1)

不使用字符串,但没有理由不能直接执行此操作:

bool Condition = (A == B);
if (chechbox1.Checked)  
{  
    Condition = Condition && (B == C);
}  
if (chechbox2.Checked)  
{  
    Condition = Condition && (C == D);  
}  
if (Condition)  
{  
    //do something  
}