locked
Declaring variable sized array in C#

    Question

  •  I an used to declaring a variable sized array in other languages using the following syntax

    int myarray[ ];

    Looks like in C# I have to specify a size.  What if I do not know this size until run time, how do I declare such an array

    Thanks in advance.

    Klaus

    Sunday, March 25, 2007 3:43 PM

Answers

  • I'd probably use an instance of List<int> instead.. This way my collection can grow/shrink/whatever... And everytime i need an array representation of the collection i call ToArray()...
    Sunday, March 25, 2007 6:24 PM
  • For more information about generics in C#, see: http://msdn2.microsoft.com/en-us/library/0x6a29h6.aspx

    Thanks

    Monday, March 26, 2007 4:10 AM
  • In C# the syntax is slightly different:

    int[] myarray = null;
    // ...
    int arraySize = 1;
    // ...
    myarray = new int[arraySize];
    Sunday, March 25, 2007 4:00 PM

All replies