If I have an array of bytes created byte[] binBuffer = new byte[256] and I fill up 100 bytes of the array, if I want to pass only those 100 bytes to some other method, is it possible to do that without creating a new byte array of 100 bytes, copying from the old array to the new, then passing off the new array? Is there somehow I can just pass the first 100 bytes. My application specifically applies to passing the array to a stored procedure.
A very common pattern when working with buffers is the:
Foo(byte[] data, int offset, int count) {...}
pattern. However, you cannot use this with a SqlCommand
/ parameter, since when you assign to a parameter it consumes the entire buffer. You will need a new array:
byte[] second = new byte[100];
Buffer.BlockCopy(first, firstOffset, second, 0, 100);
param.Value = second;
If you can use linq:
SomeMethod(binBuffer.Take(100));
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