Almost array offset

I am using C #. I have an array of size 10. I want to pass it to a function, but only from the second element. In C, this is how I implement it.

myfunc (myarray + 1)

In fact, I am actually moving the array / deleting the first element.

How to implement this in C #?

+3
source share
3 answers

If you are using .NET 3.5, the easiest way is to use Skip (1) and then convert back to an array.

myFunc(myArray.Skip(1).ToArray());

If performance is a problem, you will either need to create a new array manually, or change your function to accept an index parameter.

+4
source

There are four options:

  • Go to the index and use it in your function
  • IEnumerable<T> (T[]), myArray.Skip(1).
  • skip, . .
  • ArraySegment<T> .

, myfunc. , , , .

+4

You can also pass an index to a function, and only access the array starting at that index.

+1
source

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


All Articles