简体   繁体   中英

Increment pointer of byte array in C#

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?

You can slice arrays in c#8 using [1..] .

var data = new char[] {'!','H','e','l','l','o',' ','W','o','r','l','d','!'};
foreach(var ch in data[1..]) Console.Write(ch);
Console.WriteLine();

gives: Hello World!

Arrays have immutable length, so either start your loops and whatnot from index 1 instead of 0 or copy it to another one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM