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 would implement it
myfunc( myarray + 1 )
Effectively I am virtually shifting the array / deleting the first element.
How do I implement this in C# ?
If you're using .NET 3.5, the easiest is to use Skip(1) then convert back to an array.
myFunc(myArray.Skip(1).ToArray());
If performance is an issue then you will either need to construct a new array manually or change your function to accept an index parameter.
There are four options here:
IEnumerable<T> instead of an array (T[]), then use myArray.Skip(1).ArraySegment<T> instead of an array for your function.This really depends on whether you have control over the usage within your myfunc function. If that function must accept an array, and you can't pass an index, you're going to be stuck creating a copy of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With