有没有一种方法可以减少使用的if语句的数量?

时间:2019-01-18 15:41:24

标签: c# if-statement

所以从本质上讲,如果要使用if语句,我会尝试减少数量。

我需要特定于两种证书类型的消息。这就是造成此问题的原因。我可以使用||和&&运算符,但随后我需要该消息有点模糊,这是我不希望的。

Switch语句不起作用,因为我使用的是>和< 我尝试使用?但是无法解决这个问题。

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语句,但要保留特定消息。

1 个答案:

答案 0 :(得分:2)

如果您打算通过使用模式匹配来提高可读性,则可以使用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;
}