如何在 C# 中缩短 if-else-if 语句

How to shorten if-else-if statement in c#

有人可以缩短这段代码吗? 有 14 个按钮和 8 个文本框。如果文本框不为空,将执行该操作,如果它不为空,则当您单击它时,与文本框中字母对应的按钮将再次可见,使文本框为空。

 private void txt1_Click(object sender, EventArgs e)
 {
        if (txt1.Text == "J")
        {
            txt1.Text = "";
            btn1.Visible = true;
        }
        else if (txt1.Text == "M")
        {
            txt1.Text = "";
            btn2.Visible = true;
        }
        else if (txt1.Text == "Y")
        {
            txt1.Text = "";
            btn3.Visible = true;
        }
        else if (txt1.Text == "E")
        {
            if (btn4.Visible == true)
            {
                txt1.Text = "";
                btn5.Visible = true;
            }
            else
            {
                txt1.Text = "";
                btn4.Visible = true;
            }
        }
        else if (txt1.Text == "Q")
        {
            txt1.Text = "";
            btn6.Visible = true;
        }
        else if (txt1.Text == "L")
        {
            if (btn7.Visible == true)
            {
                txt1.Text = "";
                btn10.Visible = true;
            }
            else
            {
                txt1.Text = "";
                btn7.Visible = true;
            }
        }
        else if (txt1.Text == "B")
        {
            txt1.Text = "";
            btn8.Visible = true;
        }
        else if (txt1.Text == "C")
        {
            txt1.Text = "";
            btn9.Visible = true;
        }
        else if (txt1.Text == "P")
        {
            txt1.Text = "";
            btn11.Visible = true;
        }
        else if (txt1.Text == "I")
        {
            txt1.Text = "";
            btn12.Visible = true;
        }
        else if (txt1.Text == "K")
        {
            txt1.Text = "";
            btn13.Visible = true;
        }
        else if (txt1.Text == "O")
        {
            txt1.Text = "";
            btn14.Visible = true;
        }
}

看看这个:我建议你回去想想更合乎逻辑的方式来做你正在做的事情。

根据我的经验,代码中任何巨大的 if-else 链都表示某处存在逻辑问题。

另外,熟悉 switch 语句

假设所有 btn 变量都是 class 状态的一部分,那么您可以像这样声明一个方法:

public Button Click(String txt) {
    switch(txt) {
        case "J":
            return btn1;
        case "M":
            return btn2;
        case "Y":
            return btn3;
        case "E":
            return (btn4.Visible ? btn5 : btn4);
        case "Q":
            return btn6;
        case "L":
            return (btn7.Visible ? btn10 : btn7);
        case "B":
            return btn8;
        case "C":
            return btn9;
        case "P":
            return btn11;
        case "I":
            return btn12;
        case "K":
            return btn13;
        case "O":
            return btn14;
    }
    return null;
}

然后你称它为:

var button = Click(txt1.Text);
if(button != null) {
    button.Visible = true;
    txt1.Text = "";
}

但是,如果 btn 变量具有局部作用域,那么您可以定义一个内联 Func<String,Button> 委托,而不是方法:

Func<String, Button> Click = txt => {
    switch(txt) {
        ...
    }
};

您仍然需要处理特殊情况("E""L"),但您可以使用 Dictionary<string, Button> 来进行查找:

var buttonDictionary = new Dictionary<string, Button>();
buttonDictionary["J"] = btn1;
buttonDictionary["M"] = btn2;
//etc...

if (buttonDictionary.Keys.Contains(txt1.Text))
{
    txt1.Text = "";
    buttonDictionary[txt1.Text].Visible = false;
}

这将减少大部分重复代码。