在c#代码中需要帮助

Vai*_*ain -1 .net c# generics

我有一个功能

protected void bindCurrencies(DropDownList drp)
    {
        drp.DataSource = dtCurrencies;
        drp.DataTextField = "CurrencyName";
        drp.DataValueField = "CurrencyID";
        drp.DataBind();
        drp.Items.Insert(0, new ListItem("Please Select"));
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用此绑定下拉列表.但有时我还需要绑定一个ListBox.我不想为listbox编写不同的函数.我该怎么做 我认为在这里使用泛型方法.但我对泛型不了解.

Fre*_*örk 9

无论DropDownListListBox继承ListControl,因此,如果你改变你的函数取ListControl为参数,它应该有两种类型的正常工作:

protected void bindCurrencies(ListControl drp)
{
    drp.DataSource = dtCurrencies;
    // and so on...
Run Code Online (Sandbox Code Playgroud)