<p>I have a two dimensional array that I need to load data into.  I know the width of the data (22 values) but I do not know the height (estimated around 4000 records, but variable).</p>

<p>I have it declared as follows:  </p>

<pre><code>float[,] _calibrationSet;
    ....
int calibrationRow = 0;
While (recordsToRead)
{
  for (int i = 0; i &lt; SensorCount; i++)
   {
     _calibrationSet[calibrationRow, i] = calibrationArrayView.ReadFloat();
   }
   calibrationRow++;
}
</code></pre>

<p>This causes a NullReferenceException, so when I try to initialize it like this:  </p>

<pre><code>_calibrationSet = new float[,];
</code></pre>

<p>I get an "Array creation must have array size or array initializer."</p>

<p>Thank you,
Keith</p>

[{'answer_id': 50581, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 4, 'selected': True, 'text': '<p>You can\'t use an array.\nOr rather, you would need to pick a size, and if you ended up needing more then you would have to allocate a new, larger, array, copy the data from the old one into the new one, and continue on as before (until you exceed the size of the new one...)</p>\n\n<p>Generally, you would go with one of the collection classes - ArrayList, List&lt;>, LinkedList&lt;>, etc. - which one depends a lot on what you\'re looking for; List will give you the closest thing to what i described initially, while LinkedList&lt;> will avoid the problem of frequent re-allocations (at the cost of slower access and greater memory usage). </p>\n\n<p>Example:</p>\n\n<pre><code>List&lt;float[]&gt; _calibrationSet = new List&lt;float[]&gt;();\n\n// ...\n\nwhile (recordsToRead)\n{\n    float[] record = new float[SensorCount];\n    for (int i = 0; i &lt; SensorCount; i++)\n    {\n        record[i] = calibrationArrayView.ReadFloat();\n    }\n    _calibrationSet.Add(record);\n}\n\n// access later: _calibrationSet[record][sensor]\n</code></pre>\n\n<p>Oh, and it\'s worth noting (as <a href="https://stackoverflow.com/questions/50558/how-do-you-initialize-a-2-dimensional-array-when-you-do-not-know-the-size#50591">Grauenwolf</a> did), that what i\'m doing here doesn\'t give you the same memory structure as a single, multi-dimensional array would - under the hood, it\'s an array of references to other arrays that actually hold the data. This speeds up building the array a good deal by making reallocation cheaper, but can have an impact on access speed (and, of course, memory usage). Whether this is an issue for you depends a lot on what you\'ll be doing with the data after it\'s loaded... and whether there are two hundred records or two million records.</p>\n'}, {'answer_id': 50582, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 2, 'selected': False, 'text': "<p>You can't create an array in .NET (as opposed to declaring a reference to it, which is what you did in your example) without specifying its dimensions, either explicitly, or implicitly by specifying a set of literal values when you initialize it. (e.g. int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };)</p>\n\n<p>You need to use a variable-size data structure first (a generic list of 22-element 1-d arrays would be the simplest) and then allocate your array and copy your data into it after your read is finished and you know how many rows you need.</p>\n"}, {'answer_id': 50584, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 0, 'selected': False, 'text': "<p>I generally use the nicer collections for this sort of work (List, ArrayList etc.) and then (if really necessary) cast to T[,] when I'm done.</p>\n"}, {'answer_id': 50588, 'author': 'ShoeLace', 'author_id': 3825, 'author_profile': 'https://Stackoverflow.com/users/3825', 'pm_score': 0, 'selected': False, 'text': "<p>you would either need to preallocate the array to a Maximum size (float[999,22] ) , or use a different data structure.</p>\n\n<p>i guess you could copy/resize on the fly.. (but i don't think you'd want to)</p>\n\n<p>i think the List sounds reasonable.</p>\n"}, {'answer_id': 50591, 'author': 'Jonathan Allen', 'author_id': 5274, 'author_profile': 'https://Stackoverflow.com/users/5274', 'pm_score': 1, 'selected': False, 'text': '<p>I would just use a list, then convert that list into an array.</p>\n\n<p>You will notice here that I used a jagged array (float[][]) instead of a square array (float [,]). Besides being the "standard" way of doing things, it should be much faster. When converting the data from a list to an array you only have to copy [calibrationRow] pointers. Using a square array, you would have to copy [calibrationRow] x [SensorCount] floats.</p>\n\n<pre><code>        var tempCalibrationSet = new List&lt;float[]&gt;();\n        const int SensorCount = 22;\n        int calibrationRow = 0;\n\n        while (recordsToRead())\n        {\n            tempCalibrationSet[calibrationRow] = new float[SensorCount];\n\n            for (int i = 0; i &lt; SensorCount; i++)\n            {\n                tempCalibrationSet[calibrationRow][i] = calibrationArrayView.ReadFloat();\n            } calibrationRow++;\n        }\n\n        float[][] _calibrationSet = tempCalibrationSet.ToArray();\n</code></pre>\n'}, {'answer_id': 50594, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 0, 'selected': False, 'text': '<p>You could also use a two-dimensional ArrayList (from System.Collections) -- you create an ArrayList, then put another ArrayList inside it.  This will give you the dynamic resizing you need, but at the expense of a bit of overhead.</p>\n'}]
['https://Stackoverflow.com/questions/50558', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1048/']