[英]Passing array by reference to extension methods
如何通過引用將數組傳遞給擴展方法。
這是我嘗試但不工作的。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.