C# 有没有办法减少使用if语句的数量?
所以本质上,我试图减少使用if语句的数量 我需要消息特定于这两种证书类型。这就是问题的根源。我可以使用| |和&&运算符,但是我需要消息有点模糊,这是我不想要的 Switch语句不起作用,因为我使用了>和< 我试过用吗?但是我的头脑无法控制它C# 有没有办法减少使用if语句的数量?,c#,if-statement,C#,If Statement,所以本质上,我试图减少使用if语句的数量 我需要消息特定于这两种证书类型。这就是问题的根源。我可以使用| |和&&运算符,但是我需要消息有点模糊,这是我不想要的 Switch语句不起作用,因为我使用了>和< 我试过用吗?但是我的头脑无法控制它 if (compatibilityCerts > 2 && nonCompCerts > 2) { rtbSummaryLog.AppendText(string.Format("There Are More Than
if (compatibilityCerts > 2 && nonCompCerts > 2)
{
rtbSummaryLog.AppendText(string.Format("There Are More Than Two Compatibility And Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "More Than Two Compatibility And Non-Compatibility Certificates Detected";
}
else if (compatibilityCerts == 0 && nonCompCerts == 0)
{
rtbSummaryLog.AppendText(string.Format("There Are No Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "No Certificates Detected";
}
else if (compatibilityCerts < 2 || nonCompCerts < 2)
{
rtbSummaryLog.AppendText(string.Format("There Are Certificates Missing On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "No Certificates Detected";
}
else
{
if (compatibilityCerts > 2)
{
rtbSummaryLog.AppendText(string.Format("There Are More Than Two Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "More Than Two Compatibility Certificates Detected";
}
else if (nonCompCerts > 2)
{
rtbSummaryLog.AppendText(string.Format("There Are More Than Two Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "More Than Two Non-Compatibility Certificates Detected";
}
else if (compatibilityCerts == 0)
{
rtbSummaryLog.AppendText(string.Format("There Are No Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "No Compatibility Certificates Detected";
}
else if (nonCompCerts == 0)
{
rtbSummaryLog.AppendText(string.Format("There Are No Non-Compatibility Certificates On This Smart Card. {0}", Environment.NewLine));
pctCertificateQuantity.BackColor = Color.Red;
lblCertificateQuantity.Text = "No Non-Compatibility Certificates Detected";
}
else
{
pctCertificateQuantity.BackColor = Color.LightGreen;
lblCertificateQuantity.Text = "Number of Certificates is Valid";
}
}
需要减少if语句,但保留特定消息。如果您的目的是通过使用模式匹配来提高可读性,则可以使用switch
var pair = new {Compatibilty=compatibilityCerts ,NonCompatibilty=nonCompCerts };
switch(pair)
{
case var item when item.Compatibilty > 2 && item.NonCompatibilty > 2:
// Do Something
break;
case var item when item.Compatibilty == 2 && item.NonCompatibilty == 2:
// Do Something
break;
case var item when item.Compatibilty < 2 && item.NonCompatibilty < 2:
// Do Something
break;
case var item when item.Compatibilty > 2 :
// Do Something
break;
case var item when item.NonCompatibilty > 2 :
// Do Something
break;
case var item when item.Compatibilty == 0 :
// Do Something
break;
default:
// Do Something
break;
}
嗯,如果代码有效,那么这可能是离题的。很遗憾,也许可以尝试一下代码复查SE?@jdv。此代码中没有足够的上下文来进行适当的审阅。它将关闭CR。这听起来像是提供上下文的提示,因为我认为任何审查过程都需要它。我能给你的唯一建议就是用英语写下关键分支的逻辑,看看是否有任何测试失败。此外,静态分析器和IDE也非常有用。我们不会教你布尔数学