What is the equivalent to VB.NET ReDim Preserve in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main()
    {
        int[] arr = new int[] { 1, 2, 3, 4 };

        for (int i = 0; i < arr.Length; i++) {
            Console.Write("{0, 3}", arr[i]);
        }

        // Resize the array while preserving its contents
        Array.Resize(ref arr, 6);

        Console.WriteLine();
        for (int i = 0; i < arr.Length; i++) {
            Console.Write("{0, 3}", arr[i]);
        }
    }
}

 
/*
run:
     
  1  2  3  4
  1  2  3  4  0  0
 
*/

 



answered Mar 23, 2025 by avibootz

Related questions

3 answers 406 views
1 answer 261 views
1 answer 137 views
1 answer 154 views
1 answer 128 views
128 views asked Aug 9, 2023 by avibootz
1 answer 308 views
308 views asked Feb 9, 2019 by avibootz
1 answer 71 views
...