• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

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:

Dim YourArray() As Integer
ReDim Preserve YourArray(i)
int[] YourArray;
//C# 2005 and beyond:
Array.Resize(ref YourArray, i + 1);

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

Use Instant C# to convert from VB.NET to C#.     

Additional resource: VB.NET and C# Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.