I've been toying around with some .NET features (namely Pipelines, Memory, and Array Pools) for high speed file reading/parsing. I came across something interesting while playing around with Array.Copy, Buffer.BlockCopy and ReadOnlySequence.CopyTo. The IO Pipeline reads data as byte and I'm attempting to efficiently turn it into char.
While playing around with Array.Copy I found that I am able to copy from byte[] to char[] and the compiler (and runtime) are more than happy to do it.
char[] outputBuffer = ArrayPool<char>.Shared.Rent(inputBuffer.Length); Array.Copy(buffer, 0, outputBuffer, 0, buffer.Length); This code runs as expected, though I'm sure there are some UTF edge cases not properly handled here.
My curiosity comes with Buffer.BlockCopy
char[] outputBuffer = ArrayPool<char>.Shared.Rent(inputBuffer.Length); Buffer.BlockCopy(buffer, 0, outputBuffer, 0, buffer.Length); The resulting contents of outputBuffer are garbage. For example, with the example contents of buffer as
{ 50, 48, 49, 56, 45 } The contents of outputBuffer after the copy is
{ 12338, 14385, 12333, 11575, 14385 } I'm just curious what is happening "under the hood" inside the CLR that is causing these 2 commands to output such different results.