Array.Resize

I'm trying to dynamically resize an array of objects and getting a error:

Error 1 The type arguments for method 'System.Array.Resize<T>(ref T[], int)' cannot be inferred from the usage. Try specifying the type arguments explicitly. D:\Code\CSharp\StuntFighter2000\StuntFighter2000\StarField.cs 89 17 StuntFighter2000

I'm confused what this means my code is:

Array.Resize(ref cStarArray, 210);

cStarArray is declared as:

private Array cStarArray = Array.CreateInstance(typeof(Star), 200);

Any thoughts



Answer this question

Array.Resize

  • Umair-Shahzad

    I'll give that a try over lunch.

    I guess though what is the point of all these different ways of doing arrays Why are there standard arrays, an array class, listarray, ..., ..., ...

    All it does is lead to confusion and take c# down the same screwed up path as c++.


  • Don Cameron

    This might work

    Star[] cStarArray = new Star[200]; // instead of Array.CreateInstance( typeof( Star ), 200 );

    Array.Resize<Star>(ref cStarArray, 10);



  • AliOmar3333

    Try "Star[] cStarArray = new Array[200];" instead.

  • Caruanas

    DodgingRain,
    the System.Array class is not a different way of "doing arrays", it simply serves as the base class of all array types. There aren't many cases in which you would need to use System.Array directly, except for its static methods.

    Now, I would also encourage you to follow the advice already given in this thread, and change the way you declare your arrays. If you cannot, you can always resort to implement a non-generic Resize method yourself, it's not that hard really:

    public static void ResizeArray (ref Array a, int newSize) {
      Array b = Array.CreateInstance (a.GetType ().GetElementType (), newSize);
      int toCopy = Math.Min (a.Length, newSize);
      if (toCopy > 0) {
        Array.Copy (a, b, toCopy);
      }
      a = b;
    }

    This is not too different from what Array.Resize<> does anyway, minus the generic.

    HTH
    --mc


  • Melvin McClurkin

    DodgingRain wrote:
    Not the same type so that isn't going to work.
    Not the same type of array, but say content. Array will work with Star[]. If you're using Array.CreateInstance because of the class Array in Array.Resize, you don't have to. For example:

    Star[] stars = new Star[200];

    Array.Resize(ref stars, 210);

    If you need to use Array.CreateInstance for some reason, you can just cast it's return to the correct type of array:

    Star[] stars = Array.CreateInstance(typeof(Star), 200) as Star[];

    Array.Resize(ref stars, 210);



  • jkh1978

    Thanks to Mario Cossi.

    This works also for multidimensional arrays (here a string[]).
    I just want to add that here, in case somebody looks for that.

    Code Block

    int[] allDimensions = new int[2] { newSize, 4 };
    Array b = Array.CreateInstance(a.GetType().GetElementType(), allDimensions);


    int toCopy = Math.Min(a.GetLength(0), newSize); // GetLength of the wanted dimension

    if (toCopy > 0)
    {

    // * 4 because a fixed number of 4 elements in each 2nd dimension

    Array.Copy(a, b, toCopy * 4);

    }


    a = (string[,])b;



  • gs272

    Thanks, Mario and Mukuh,

    I found that Array.Resize is not implemented in Microsoft .NET Compact Framework 2.0.

    This thread provides me the workaround over the problem.

    Thanks a lot!

  • Brian Driscoll

    Not the same type so that isn't going to work.
  • Array.Resize