I’m wondering if constantly resizing byte arrays can take a big hit on performance. I’m adding data to a class and if the class already contains that data type I need to add it to the existing byte array, which means I’ll need to resize it. The problem is that there are some data types that I have that will be adding in bulk which means there could be multiple array resizes occurring.
Would this make a huge impact on performance? This class CAN be very performance critical.
If it does, then I might have to do a design-overhaul.
If resizing is required then you should use a
List<byte>instead. Arrays cannot be resized so you would have to create a completely new array and then copy the old content into the new array before adding additional content (this is whatArray.Resizedoes if that’s what you were referring to).List<T>is using an array internally but will optimize resizing so you don’t have to deal with it.Essentially once the internal array is full and new content is added,
List<T>will double the internal array size, hence resizing should in fact occur very rarely – if you resize your array directly on the other hand you will either have to employ a similar strategy and keep a “size counter” or take the resize performance cost on any content addition.