How to modify the size of an array

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
 
J

Jon Skeet [C# MVP]

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.
 
B

Brett Hewitson

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.
 
P

Peter Rilling

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.
 
M

Michael Culley

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.
 

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

Top