73

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.

John Saunders
  • 157,405
  • 24
  • 229
  • 388

15 Answers15

82

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.
Bo Jeanes
  • 6,029
  • 4
  • 40
  • 37
Chris Shain
  • 49,121
  • 5
  • 89
  • 122
  • If Array.Resize() is just once at the beginning (and not multiple times during the program), is that more suitable than a List? – Dan W Feb 05 '12 at 20:40
  • 5
    `List` uses an Array behind the scenes, so there are very few reasons to use an Array in C#. Lists can be initialized to a suitable size using this constructor: http://msdn.microsoft.com/en-us/library/dw8e0z9z.aspx. But yes, if your heart is set on Arrays, it's better to resize them as infrequently as possible. – Chris Shain Feb 05 '12 at 20:45
  • I thought List uses a linked list or some data structure behind. So it uses array after all. – dpp Sep 08 '12 at 18:02
  • 7
    Be carefull, Array.Resize does not preserve your reference. Instead, it creates a new array of the required size. – Loul G. Mar 18 '15 at 20:58
  • Does this resizing effect the optimization and complexity. I am using insertion sort, so when I add an element in the array, I increase the size and redo the insertion sort algorithm, so does it effect the program performance? – shubhraj Dec 21 '16 at 19:41
  • Not sure what you mean @shubhraj... `Array.Resize` creates a new array of the specified size. The beginning of the new array is the same as the old array, and the additional space is filled with the default value for the Array's type (null for reference types) – Chris Shain Dec 21 '16 at 19:45
  • Yes, I got ur answer on what the Array.Resize does. I am doing the same. I have an array & filled with random number. I sort the array in ascending order using Insertion Sort Algorithm. Now I add another integer to the array, for doing this I use the solution you mentioned above. So it reallocates more memory space to the array. Does this effect the performance of the program during runtime? I need to optimize my code, so I'm confused whether to use List or Array.Resize..Hope u got my point of what I am trying to explain.. – shubhraj Dec 21 '16 at 19:57
  • 2
    `List` is using Array.Resize under the covers. It uses a generally (though not universally) optimal algorithm under the covers, wherein it doubles the size of the array each time that it grows it. Unless you are working on high performance realtime systems (high-speed trading or mechanical control systems for example) you should use `List` as it will be "good enough" and is more maintainable. – Chris Shain Dec 21 '16 at 20:02
  • @ChrisShain so how can we delete old reference if it don't delete old reference ??? – AminM Jun 15 '17 at 11:54
  • Assuming the old array is not rooted by anything it will be garbage collected – Chris Shain Jun 15 '17 at 15:09
63

No, try using a strongly typed List instead.

For example:

Instead of using

int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;

You could do this:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);

Lists use arrays to store the data so you get the speed benefit of arrays with the convenience of a LinkedList by being able to add and remove items without worrying about having to manually change its size.

This doesn't mean an array's size (in this instance, a List) isn't changed though - hence the emphasis on the word manually.

As soon as your array hits its predefined size, the JIT will allocate a new array on the heap that is twice the size and copy your existing array across.

Joseph Woodward
  • 8,837
  • 5
  • 39
  • 61
Corey Sunwold
  • 9,892
  • 5
  • 47
  • 55
12

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)

Philip Pittle
  • 10,003
  • 7
  • 47
  • 100
DerpDerp
  • 199
  • 1
  • 11
8

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);
}
HENG Vongkol
  • 687
  • 6
  • 9
6

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()
KbManu
  • 410
  • 3
  • 7
6

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]); 
            }
    
sgokhales
  • 49,762
  • 33
  • 123
  • 158
5

Use System.Collections.Generic.List

kelloti
  • 7,985
  • 4
  • 40
  • 76
5

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);
John Saunders
  • 157,405
  • 24
  • 229
  • 388
4

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

Bowdzone
  • 3,651
  • 11
  • 39
  • 49
3
    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;
    }
1

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" });
Mohit Shrivastava
  • 12,996
  • 5
  • 31
  • 61
1

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++;
}
PhillipPDX
  • 68
  • 1
  • 10
1

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.
clamchoda
  • 3,111
  • 2
  • 30
  • 63
0

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

anObject.anArray.Concat(new string[] { newArrayItem }).ToArray();
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Miq
  • 1
  • 1
-1

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