C# counterpart to VB.NET Redim

G

Guest

Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas
 
M

Mike

Niklas said:
Hi
What is the best way to enlarge an C# array which already have values?
In VB.NET I would do
ReDim Preserve A(10)
regards
/Niklas

You need to reallocate and copy yourself. To can use the array's Copy(To)
method for this purpose.

If you don't want to do this, just use an ArrayList instead of an array and
just Add() elements all you want and let the collection take care of things
for you.

m
 
N

Nicholas Paldino [.NET/C# MVP]

Additionally, in .NET 2.0, you can just use the Generic type List<T>
which will do this as well and give you type safety.
 
G

Guest

First of all, a .NET collection such as ArrayList might be preferable.
However, if you want to resize an array, then the following is what you need:
(assuming an array of type "SomeType")

SomeType[] temp = new SomeType[11];
if (A != null)
Array.Copy(A, temp, Math.Min(A.Length, temp.Length));
A = temp;
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
W

William Stacey [MVP]

List<T> is dynamic, so you don't even need to redim.

--
William Stacey [MVP]

| Hi
| What is the best way to enlarge an C# array which already have values?
| In VB.NET I would do
| ReDim Preserve A(10)
| regards
| /Niklas
 

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