我想创建一个列表运行的扩展方法,并接受其他列表:但是ICollection的扩展方法<T>不会更改调用列表?
public static void Charge<T, S>(this ICollection<T> targetList, ICollection<S> sourceList) where T : class, new()
{
if (targetList == null || sourceList == null)
throw new NullReferenceException();
targetList = new List<T>();
foreach (var item in sourceList)
{
T t = new T();
//do work on t
targetList.Add(t);
}
}
当我这样称呼它:
var targetList = new List<Item>();
targetList.Charge(sourceList);
的targetList
不会改变(项目数= 0)
删除'targetList = new List();' –
你为什么要做'targetList = new List();'? –
rbm
您没有返回目标列表,也没有将其作为参考处理。 – Trioj