How to set the length of byte[]

  • Thread starter Thread starter flyfish
  • Start date Start date
The array length is set when you instantiate the array, and can't be
changed after that.

ArrayLists on the other hand can grow and shrink in size. But I
generally wouldn't recommend using that a replacement for a byte[]
since every byte would be boxed.



Mattias
 
You can't change the length, but you could allocate a new array of larger
size and copy current array into it.
byte[] SetLenght(byte[] currByteArray, int newSize)
{
if ( currByteArray.Length > newSize)
//error
byte[] newArray = new byte[newSize];
// copy currArray to newArray.
return newArray;
}

Other error checks are needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top