简体   繁体   English

抽象类型作为方法中的参数(.net,c#)

[英]Abstract type as parameter in method (.net, c#)

I have the following method which I use to fill a DropDownList-Control. 我有以下方法用于填充DropDownList-Control。

protected void LoadDropDownList(DropDownList ddl, IEnumerable<A> source)
{
    ddl.DataSource = source;
    ddl.DataBind();
}

My question is, can I make the method more abstract so that it can also take IEnumerables of type B? 我的问题是,我可以使该方法更抽象,以便它也可以采用类型B的IEnumerables吗?

protected void LoadDropDownList<T>(DropDownList ddl, IEnumerable<T> source) { 
    ...

See also .

protected void LoadDropDownList(DropDownList ddl, IEnumerable source) {
     ddl.DataSource = source;
     ddl.DataBind();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM