I have a byte[] data in which I don't need first element. Array is quiet big, so I don't want to copy it to another array (payload) as shown below.
Buffer.BlockCopy(data, 1, payload, 0, count);
In C++ I can just increment pointer of array to move poiner.
int main()
{
char* data= "!Hello, World!";
data++;
std::cout << data<< std::endl;
}
This part of code prints "Hello, World!". How I can do the same in C#? Maybe to use unsafe code?
data.Slice(1)will create aSpan<byte>starting at index 1 of the array and ending at its last element.Span<byte>first, I don't know if it's implied. So it should be((Span<byte>)data).Slice(1);. This will not cause a copy it's essentially a safe pointer to the array.[1..].Span<T>should be as fast as indexing into an array, if efficiency matters. It is a direct pointer as opposed toArraySegment<T>which stores a reference to the array and an offset/boundary.