C# – Define a double array without a fixed size


Hello i have a problem with c# Arrays. I need a array to store some data in there…
My Code is that

double[] ATmittelMin;
ATmittelMin[zaehlMittel] = Gradient(x, xATmax, y, yATmax);

But the compiler says: not defined var
How can i define a double array without a fixed size ?
Thanks a lot!

Best Answer

Arrays are always fixed in size, and have to be defined like so:

double[] items1 = new double[10];

// This means array is double[3] and cannot be changed without redefining it.
double[] items2 = {1.23, 4.56, 7.89};

The List<T> class uses an array in the background and redefines it when it runs out of space:

List<double> items = new List<double>();

// This will give you a double[3] array with the items of the list.
double[] itemsArray = items.ToArray();

You can iterate through a List<T> just as you would an array:

foreach (double item in items)

// Note that the property is 'Count' rather than 'Length'
for (int i = 0; i < items.Count; i++)