我已经编写了一个小扩展方法来将值添加到List的开头.
这是代码;
public static class ExtensionMethods
{
public static void AddBeginning<T>(this List<T> item, T itemValue, ref List<T> currentList)
{
List<T> tempList = new List<T> {itemValue};
tempList.AddRange(currentList);
currentList = tempList;
}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以将值添加到列表的开头,我必须使用ref
关键字.
有人建议必须修改这个扩展方法来摆脱ref关键字吗?
你可以打电话currentList.Insert(0, itemValue);
插入到开头.
编辑:
注意 - 此代码将修改列表实例,而原始代码保持列表完整无缺,并生成一个新列表,其中包含在开头插入的附加数据.
归档时间: |
|
查看次数: |
795 次 |
最近记录: |