Tuesday, April 20, 2010

Resizing array

If you ever had to work with an array that you wouldn't know the size till run-time, you have a couple of options:


  1. Clone the old array into the new array (un-necessary overhead)
  2. Just proceed with a array with redundant trailing allocated memory
  3. You can resize it with Array.Resize(ref <arrayName>, int newArraySize)


int[] myArray = new int[50];
int newArrayCount = 0;

//some biz logic goes here
//determined array size

newArrayCount = myArray.Count;

Array.Resize(ref myArray, newArrayCount);

You can put myArray.Count as the 2nd parameter of the resizing method, up to your preference.

2 comments:

  1. reading ur articles is like reading a book with tips and tricks. However as a whole, it's not cohesive and every topics are like randomly generated.

    ReplyDelete
  2. thanks for your comments, i'm very new to blogging technical stuff. I'm figuring out a way to group all my topics into a more user-friendly way. I will try to improve the content along the way.

    ReplyDelete