Array transfer by extension methods

How to pass an array to an extension method by reference.

this is what i tried but didn't work.

public static void RemoveAtIndex(ref this int[] arr, int index) 
+4
source share
2 answers

You cannot send an extension target by reference. You need it? Is the array replaced by a new extension method?

+4
source

Linq is used to return data, not to modify data. With a slightly different approach, you replace the full array and don't change the array.

First add this little extension method:

 public static class Extensions { public static IEnumerable<T> SkipAt<T>(this IEnumerable<T> source, int index) { return source.Where((it, i) => i != index); } } 

Then you can do this "Linq way":

 var a = new[] {1,2,3}; a = a.SkipAt(1).ToArray(); 
0
source

Source: https://habr.com/ru/post/1495672/


All Articles