Resize An Array

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.

			
                            using System;
                            
                            // Reallocates an array with a new size, 
                            // and copies the contents
                            // of the old array to the new array.
                            // Arguments:
                            // oldArray : the old array, to be reallocated.
                            // intnewSize : the new array size.
                            // Returns : A new array with the same contents.
                            public static Array resizeArray (Array oldArray, int intnewSize)
                            {
                               int intoldSize = oldArray.Length;
                               Type elementType = oldArray.GetType().GetElementType();
                               Array newArray = Array.CreateInstance(elementType,intnewSize);
                               int preserveLength = System.Math.Min(intoldSize,intnewSize);
                               if (preserveLength > 0)
                               Array.Copy (oldArray,newArray,preserveLength);
                               return newArray; 
                            }