Search code examples
c#arraysperformanceslicespace-efficiency

C# Array slice without copy


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.


Solution

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

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