Home > database >  c# byte[] asspan equivalent?
c# byte[] asspan equivalent?

Time:05-15

I have a line of code with an AsSpan method that didn't exist in .NET 4.7.2. This is from a netstandard2.1 project that I neither understand nor can convert to .NET 4.7.2.

I found out that the "AsSpan" method is possible using the System.Memory NuGet package. However, the Write method then requires an offset and count. Unfortunately, I have no idea about file operations.

Can someone help me please? It should run under Mono .NET 4.7.2.

class Packer
{
    public byte[] Bytes { get; }
    // ...
    public override void Pack(Stream destination)
    {
        destination.Write(Bytes.AsSpan());
    }
}

CodePudding user response:

As you suspected, the other version of Stream.Write would do the trick.

offset

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

You want to write every byte in the array, so you want to start with the first, or an offset of 0.

count

The number of bytes to be written to the current stream

You want to write every byte in the array, soy you want to write Bytes.Length bytes.

destination.Write(Bytes, 0, Bytes.Length);
  • Related