Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase byte[] size at run time?

Tags:

arrays

c#

I have to increase byte[] array size at runtime.
How to increase byte[] array size at run time ?

like image 233
jams Avatar asked May 16 '11 12:05

jams


People also ask

How large can a byte array be?

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 .

Can we increase size of array in C#?

You cannot resize an array in C#, but using Array. Resize you can replace the array with a new array of different size.

How can you resize an array in ASP?

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.

What is the maximum size of byte array in C#?

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.


2 Answers

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.

like image 127
sehe Avatar answered Sep 27 '22 22:09

sehe


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.

like image 21
Richard Avatar answered Sep 27 '22 21:09

Richard