如何通过引用将数组传递给扩展方法。通过引用来传递数组扩展方法
这是我所尝试过的,但没有工作。
public static void RemoveAtIndex(ref this int[] arr, int index)
如何通过引用将数组传递给扩展方法。通过引用来传递数组扩展方法
这是我所尝试过的,但没有工作。
public static void RemoveAtIndex(ref this int[] arr, int index)
您不能通过ref发送扩展的目标对象。 你是否需要它? 由扩展方法将数组替换为新数组吗?
Linq用于返回数据,不用于更改数据。用一个不同的方法,你可以用代替这个完整的数组,而不是改变数组。
首先添加这个小扩展方法:
public static class Extensions
{
public static IEnumerable<T> SkipAt<T>(this IEnumerable<T> source, int index)
{
return source.Where((it, i) => i != index);
}
}
然后,你可以做到这一点“Linq的方式”:
var a = new[] {1,2,3};
a = a.SkipAt(1).ToArray();
Linq!==扩展方法 – WickyNilliams
是的,我需要它和阵列是由新 –
如此替换你不能使用扩展方法来实现这个目标 –
有没有其他方法....? –