Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using part of a byte array

Tags:

c#

types

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.

like image 806
Jeremy Avatar asked Feb 26 '09 04:02

Jeremy


2 Answers

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;
like image 155
Marc Gravell Avatar answered Oct 22 '22 23:10

Marc Gravell


If you can use linq:

SomeMethod(binBuffer.Take(100));
like image 29
Chris Shaffer Avatar answered Oct 22 '22 23:10

Chris Shaffer