C# Equivalent to VB ReDim Preserve





























C# Equivalent to VB ReDim Preserve

Resizing arrays while preserving the existing contents is easily done in VB using ReDim Preserve.  In C#, you have other alternatives:

  • Use one of the existing .NET collections, such as ArrayList or the generic List, that handle dynamic resizing automatically.
  • If the Array has only one dimension and you're using C# 2005/2008, then you can use the Array.Resize method.
  • Code the same sort of logic that VB executes for a ReDim Preserve:

VB:
Dim YourArray() As Integer
...
ReDim Preserve YourArray(i)

C#:
int[] YourArray;
...
int[] temp = new int[i + 1];
if (YourArray != null)
    Array.Copy(YourArray, temp, Math.Min(YourArray.Length, temp.Length));
YourArray = temp;

If you need to convert between VB.NET and C# and you are depending on the results being reliable and accurate, then you will want to have Instant C#, the best VB.NET to C# converter, or Instant VB, the best C# to VB.NET converter, at your fingertips.

 

 
 
Email your questions and comments to:  info@tangiblesoftwaresolutions.com

[Site Map]        [Home]


Copyright © 1997 - 2009 Tangible Software Solutions Inc.