~~~请教IList,List分别在什么情况下使用比较合适?

finer 2017-11-18 08:36:49
像下面方法的返回类型用的是IList,里面也是IList,可以用List吗,为什么不用List的呢?谢谢

public class ModelConvertHelper<T> where T : new() // 此处一定要加上new()
{

public static IList<T> ConvertToModel(DataTable dt)
{

IList<T> ts = new List<T>();// 定义集合
Type type = typeof(T); // 获得此模型的类型
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
...全文
213 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2017-11-20
  • 打赏
  • 举报
回复
List继承自IList List功能比较丰富,IList相对功能就少了。 一般来说,如果只是查询数据集合,没有数据操作的话,用IList。 如果有数据的操作(比较典型的就是 sort方法了。IList没有sort方法),那么用List。
You冷 2017-11-20
  • 打赏
  • 举报
回复
https://jingyan.baidu.com/article/c85b7a6410e6e2003bac95c9.html
You冷 2017-11-20
  • 打赏
  • 举报
回复
这个你可以去搜一下IList和List的区别就知道了

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧