Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello everyone, is there a way to simulate VBs redim function in C#?


In VB, redim is used to resize array. There is no need for that in C#, you use a List whose size is dynamically increased as required, see list[^]
Share this answer
For a single-dimension Array, yes, you can directly simulate VB .NET's 'redim, and 'redim preserve,' functionality by using: Array.Resize(ref NameOFArray, #newSize);

VB .NET's 'redim allows you to change the size of any dimension in a multi-dimensional Array; to achieve that in C# requires you write a custom method to build a new Array of the required number of dimensions (rank), and copy over the appropriate values from the array-being-copied.

See: C# Array.Resize: [^].

In the article linked to above, you will find sample code for simulating VB 'redim in C# with a multi-dimensional Array.

Do keep in mind that if you have references to an existing Array, and then resize the Array in C#, the references are not updated/changed ! I suggest you carefully study Jon Skeet's example of this, here: [^].

The issue of whether or not any use of the Array object in .NET should be changed to use a Generic List based structure is an interesting one. For me, the answer is "no;" I believe there are many situations where using Arrays is quite efficient, and useful.

But, imho, you should really examine the different viewpoints on this; I suggest you examine the first six results of this Google search: [^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900