How to modify the size of an array

  • Thread starter Thread starter Brett Hewitson
  • Start date Start date
B

Brett Hewitson

Hello,

In Visual Basic you can redim an array to increase or decrease it's size. I
am not sure how to do this in c#.

At the moment all I can think of is the following:

1. Create a new array of the new size
2. Copy the old array into the new array
3. Null the old array.

Is there a better way to do this?

Brett
 
Brett Hewitson said:
In Visual Basic you can redim an array to increase or decrease it's size. I
am not sure how to do this in c#.

At the moment all I can think of is the following:

1. Create a new array of the new size
2. Copy the old array into the new array
3. Null the old array.

Is there a better way to do this?

Yes - take out step three as it's unnecessary. Just do something like:

string[] tmp = new string[newLength];
myArray.CopyTo(tmp, 0);

That's all you need.

That's what VB.NET is doing behind the scenes anyway, by the way - all
arrays in .NET are fixed size.
 
Jon,

Thanks for the help and also the information. I am glad that I had the
right idea.

Brett

Jon Skeet said:
Brett Hewitson said:
In Visual Basic you can redim an array to increase or decrease it's size. I
am not sure how to do this in c#.

At the moment all I can think of is the following:

1. Create a new array of the new size
2. Copy the old array into the new array
3. Null the old array.

Is there a better way to do this?

Yes - take out step three as it's unnecessary. Just do something like:

string[] tmp = new string[newLength];
myArray.CopyTo(tmp, 0);

That's all you need.

That's what VB.NET is doing behind the scenes anyway, by the way - all
arrays in .NET are fixed size.
 
Although, if you find that you need to continually adjust the size of an
array, you might consider using an ArrayList.


Brett Hewitson said:
Jon,

Thanks for the help and also the information. I am glad that I had the
right idea.

Brett

Jon Skeet said:
Brett Hewitson said:
In Visual Basic you can redim an array to increase or decrease it's size. I
am not sure how to do this in c#.

At the moment all I can think of is the following:

1. Create a new array of the new size
2. Copy the old array into the new array
3. Null the old array.

Is there a better way to do this?

Yes - take out step three as it's unnecessary. Just do something like:

string[] tmp = new string[newLength];
myArray.CopyTo(tmp, 0);

That's all you need.

That's what VB.NET is doing behind the scenes anyway, by the way - all
arrays in .NET are fixed size.
 
Make sure you don't do this for every element as it will be slow. The other
alternative is to use an arraylist.

Brett Hewitson said:
Jon,

Thanks for the help and also the information. I am glad that I had the
right idea.

Brett

Jon Skeet said:
Brett Hewitson said:
In Visual Basic you can redim an array to increase or decrease it's size. I
am not sure how to do this in c#.

At the moment all I can think of is the following:

1. Create a new array of the new size
2. Copy the old array into the new array
3. Null the old array.

Is there a better way to do this?

Yes - take out step three as it's unnecessary. Just do something like:

string[] tmp = new string[newLength];
myArray.CopyTo(tmp, 0);

That's all you need.

That's what VB.NET is doing behind the scenes anyway, by the way - all
arrays in .NET are fixed size.
 
Back
Top