[c#] change array size

Is it possible to change an array size after declaration? If not, is there any alternative to arrays?
I do not want to create an array with a size of 1000, but I do not know the size of the array when I'm creating it.

This question is related to c# arrays size

The answer is


Use a List<T> instead. For instance, instead of an array of ints

private int[] _myIntegers = new int[1000];

use

private List<int> _myIntegers = new List<int>();

later

_myIntegers.Add(1);

Use a generic List (System.Collections.Generic.List).


This worked well for me to create a dynamic array from a class array.

var s = 0;
var songWriters = new SongWriterDetails[1];
foreach (var contributor in Contributors)
{
    Array.Resize(ref songWriters, s++);
    songWriters[s] = new SongWriterDetails();
    songWriters[s].DisplayName = contributor.Name;
    songWriters[s].PartyId = contributor.Id;
    s++;
}

You can use Array.Resize(), documented in MSDN.

But yeah, I agree with Corey, if you need a dynamically sized data structure, we have Lists for that.

Important: Array.Resize() doesn't resize the array (the method name is misleading), it creates a new array and only replaces the reference you passed to the method.

An example:

var array1 = new byte[10];
var array2 = array1;
Array.Resize<byte>(ref array1, 20);

// Now:
// array1.Length is 20
// array2.Length is 10
// Two different arrays.

You can use Array.Resize() in .net 3.5 and higher. This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one. (So you will need the memory available for both arrays as this probably uses Array.Copy under the covers)


    private void HandleResizeArray()
    {
        int[] aa = new int[2];
        aa[0] = 0;
        aa[1] = 1;

        aa = MyResizeArray(aa);
        aa = MyResizeArray(aa);
    }

    private int[] MyResizeArray(int[] aa)
    {
        Array.Resize(ref aa, aa.GetUpperBound(0) + 2);
        aa[aa.GetUpperBound(0)] = aa.GetUpperBound(0);
        return aa;
    }

In case you cannot use Array.Reset (the variable is not local) then Concat & ToArray helps:

anObject.anArray.Concat(new string[] { newArrayItem }).ToArray();


Use a List (where T is any type or Object) when you want to add/remove data, since resizing arrays is expensive. You can read more about Arrays considered somewhat harmful whereas a List can be added to New records can be appended to the end. It adjusts its size as needed.

A List can be initalized in following ways

Using collection initializer.

List<string> list1 = new List<string>()
{
    "carrot",
    "fox",
    "explorer"
};

Using var keyword with collection initializer.

var list2 = new List<string>()
{
    "carrot",
    "fox",
    "explorer"
};

Using new array as parameter.

string[] array = { "carrot", "fox", "explorer" };
List<string> list3 = new List<string>(array);

Using capacity in constructor and assign.

List<string> list4 = new List<string>(3);
list4.Add(null); // Add empty references. (Not Recommended)
list4.Add(null);
list4.Add(null);
list4[0] = "carrot"; // Assign those references.
list4[1] = "fox";
list4[2] = "explorer";

Using Add method for each element.

List<string> list5 = new List<string>();
list5.Add("carrot");
list5.Add("fox");
list5.Add("explorer");

Thus for an Object List you can allocate and assign the properties of objects inline with the List initialization. Object initializers and collection initializers share similar syntax.

class Test
{
    public int A { get; set; }
    public string B { get; set; }
}

Initialize list with collection initializer.

List<Test> list1 = new List<Test>()
{
    new Test(){ A = 1, B = "Jessica"},
    new Test(){ A = 2, B = "Mandy"}
};

Initialize list with new objects.

List<Test> list2 = new List<Test>();
list2.Add(new Test() { A = 3, B = "Sarah" });
list2.Add(new Test() { A = 4, B = "Melanie" });

In C#, Array.Resize is the simplest method to resize any array to new size, e.g.:

Array.Resize<LinkButton>(ref area, size);

Here, i want to resize the array size of LinkButton array:

<LinkButton> = specifies the array type
ref area = ref is a keyword and 'area' is the array name
size = new size array


Yes, it is possible to resize an array. For example:

int[] arr = new int[5];
// increase size to 10
Array.Resize(ref arr, 10);
// decrease size to 3
Array.Resize(ref arr, 3);

If you create an array with CreateInstance() method, the Resize() method is not working. For example:

// create an integer array with size of 5
var arr = Array.CreateInstance(typeof(int), 5);
// this not work
Array.Resize(ref arr, 10);

The array size is not dynamic, even we can resize it. If you want a dynamic array, I think we can use generic List instead.

var list = new List<int>();
// add any item to the list
list.Add(5);
list.Add(8);
list.Add(12);
// we can remove it easily as well
list.Remove(5);
foreach(var item in list)
{
  Console.WriteLine(item);
}

In C#, arrays cannot be resized dynamically.

  • One approach is to use System.Collections.ArrayList instead of a native array.

  • Another (faster) solution is to re-allocate the array with a different size and to copy the contents of the old array to the new array.

    The generic function resizeArray (below) can be used to do that.

    public static System.Array ResizeArray (System.Array oldArray, int newSize)  
        {
          int oldSize = oldArray.Length;
          System.Type elementType = oldArray.GetType().GetElementType();
          System.Array newArray = System.Array.CreateInstance(elementType,newSize);
    
          int preserveLength = System.Math.Min(oldSize,newSize);
    
          if (preserveLength > 0)
          System.Array.Copy (oldArray,newArray,preserveLength);
    
         return newArray; 
      }  
    
     public static void Main ()  
           {
            int[] a = {1,2,3};
            a = (int[])ResizeArray(a,5);
            a[3] = 4;
            a[4] = 5;
    
            for (int i=0; i<a.Length; i++)
                  System.Console.WriteLine (a[i]); 
            }
    

Used this approach for array of bytes:

Initially:

byte[] bytes = new byte[0];

Whenever required (Need to provide original length for extending):

Array.Resize<byte>(ref bytes, bytes.Length + requiredSize);

Reset:

Array.Resize<byte>(ref bytes, 0);

Typed List Method

Initially:

List<byte> bytes = new List<byte>();

Whenever required:

bytes.AddRange(new byte[length]);

Release/Clear:

bytes.Clear()

If you really need to get it back into an array I find it easiest to convert the array to a list, expand the list then convert it back to an array.

        string[] myArray = new string[1] {"Element One"};
        // Convert it to a list
        List<string> resizeList = myArray.ToList();
        // Add some elements
        resizeList.Add("Element Two");
        // Back to an array
        myArray = resizeList.ToArray();
        // myArray has grown to two elements.