简体   繁体   中英

Passing array by reference to extension methods

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

this is what i have tried but not working.

public static void RemoveAtIndex(ref this int[] arr, int index)

You can't send extension's target object by ref. Do you relly need it? Is array replaced by the new one by the extension method?

Linq is used to return data, not to change data. With a little different approach you replace the complete array and not changing 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 could do it the "Linq way":

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM