Search code examples
c#jagged-arrays

Jagged Array Dimensions


I'm using jagged arrays and have a bit of a problem (or at least I think I do). The size of these arrays are determined by how many rows are returned in a database, so it is very variable. The only solution I have is to set the dimensions of the array to be very huge, but this seems ... just not the best way to do things.

int[][] hulkSmash = new int[10][];
hulkSmash[0] = new int[2] {1,2};

How can I work with jagged arrays without setting the dimensions, or is this just a fact of life with C# programming??


Solution

  • List<List<int>> hulkSmash = new List<List<int>>();
    hulkSmash.Add(new List<int>() { 1, 2 });
    

    or

    List<int[]> hulkSmash = new List<int[]>();
    hulkSmash.Add(new int[] { 1, 2 });