方法(Object sender,EventAgrs e)和方法(control c)哪个参数类型更好



我对这两种类型的参数传递有很大的混淆。两者看起来都一样。我在这里发送我的代码,这两个方法将执行相同的功能。但我不知道哪种编程方法是有效的。

    public void remvuserpro(ListBox lb1)
    {
        string temp1 = "";
        foreach (KeyValuePair<string, int> rm in purchase)
        {
            string temp=rm.Key+"t"+rm.Value;
            if (temp ==lb1.SelectedItem.ToString())
            {
                temp1 = rm.Key;
               lb1.Items.Remove(lb1.SelectedItem);
               billtotal -= rm.Value;
               label3.Text = Convert.ToString(billtotal);
               break;
            } 
        }
        purchase.Remove(temp1);
    }
    public void rmv(object sender, EventArgs e)
    {
        ListBox lb1 = sender as ListBox;
        string temp1 = "";
        foreach (KeyValuePair<string, int> rm in purchase)
        {
            string temp = rm.Key + "t" + rm.Value;
            if (temp == lb1.SelectedItem.ToString())
            {
                temp1 = rm.Key;
                lb1.Items.Remove(lb1.SelectedItem);
            }
        }
        purchase.Remove(temp1);
    }

在大多数情况下,第一个是正确的。第二个是针对事件。在您的情况下,您希望这两个方法做相同的事情,所以您应该重用自己的代码。

类似这样的东西:

public void rmv(object sender, EventArgs e)
{
    ListBox lb1 = sender as ListBox;
    if(lb1 != null)
        remvuserpro(lb1);
}

换句话说,第二个方法是为您自动生成的,但是,您不应该这样设计自己的方法。

最新更新