tags:

views:

833

answers:

5

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: 

If you can use linq:

SomeMethod(binBuffer.Take(100));
Chris Shaffer
I'm too lazy to check -- but does that really NOT just copy the array to a new array of size 100?
Andrew Flanagan
Agree with Andrew. This should be double-checked.
siz
The docs state the method uses deferred execution, the query represented is not exec'd until the object is enumerated with GetEnumerator or foreach. Take(src) enumerates source and yields elements until count elements are yielded or src contains no more elements. So I'm guessing it doesn't copy.
Jeremy
But I'd have to call ToArray at the end, which would make a copy anyhow.
Jeremy
A: 

if you are using .net 3.5 you can use the Take() extension method and do the following:

class Program
{
    static void Main(string[] args)
    {
        byte[] b = new byte[1000];

        dowork(b.Take(10).ToArray());

    }

    public static void dowork(byte[] b)
    {
        // do some work
    }
}
Jared
If you downvote, please state why... So we can understand why the answer given is not valid.
Jeremy
+1  A: 

When an array is passed to a method, only a reference to it is actually passed, since arrays are actually reference types. You basically have a pointer to the array, and then the offset in the square brackets just tells you how many "slots" away from the start. Therefore, the only real way to do this would be to pass two parameters to your method. One being the actual array, and the second being the max number until where you need to go, in your case it would be 100. In that method you then only iterate through the array until the max number is reached.

BFree
+3  A: 

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;
Marc Gravell
+1  A: 

The short answer is: NO. Anything you do (besides passing the array itself as the parameter) will create a new array and copy the first 100 bytes in it.

However, since you are not passing the array by value, but instead are passing a reference to it, why does it matter to you if the array is 256 bytes or 100? You are not wasting more memory. You might need to tell the method how much of that array to use, though, as the Length method will return the full length.

EDIT: I just realized you want to pass the array to a stored procedure. This will copy the whole array. You will have to make a copy with only the elements you want to pass.

Franci Penov