C#扩展方法,摆脱ref关键字

0 c# extension-methods

我已经编写了一个小扩展方法来将值添加到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关键字吗?

Mik*_*our 6

你可以打电话currentList.Insert(0, itemValue);插入到开头.

编辑:

注意 - 此代码将修改列表实例,而原始代码保持列表完整无缺,并生成一个新列表,其中包含在开头插入的附加数据.