C# Array slice without copy

user1400716 picture user1400716 · Jan 14, 2013 · Viewed 8.1k times · Source

I'd like to pass a sub-set of a C# array to into a method. I don't care if the method overwrites the data so would like to avoid creating a copy.

Is there a way to do this?

Thanks.

Answer

SLaks picture SLaks · Jan 14, 2013

Change the method to take an IEnumerable<T> or ArraySegment<T>.

You can then pass new ArraySegment<T>(array, 5, 2)