I have to increase byte[]
array size at runtime.
How to increase byte[] array size at run time ?
bytearray() method returns a bytearray object (i.e. array of bytes) which is mutable (can be modified) sequence of integers in the range 0 <= x < 256 .
You cannot resize an array in C#, but using Array. Resize you can replace the array with a new array of different size.
Remarks. This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one. array must be a one-dimensional array. If array is null , this method creates a new array with the specified size.
The current implementation of System. Array uses Int32 for all its internal counters etc, so the theoretical maximum number of elements is Int32. MaxValue . There's also a 2GB max-size-per-object limit imposed by the Microsoft CLR.
Why does no one seem to know Array.Resize:
Array.Resize(ref myArray, 1024);
Simple as pie.
PS: _as per a comment on MSDN, apparently this was missing from the MSDN documentation in 3.0.
You can't: arrays are fixed size.1
Either use a re-sizable collection (eg. List<byte>
) or create a new larger array and copy the contents of the original array over.
1 Even Array.Resize doesn't modify the passed array object: it creates a new array and copies the elements. It just saves you coding this yourself. The difference is important: other references to the old array will continue to see the old array.
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