Home > database >  Construct array of decimals dynamically
Construct array of decimals dynamically

Time:12-22

How can I declare an array of decimals and then add values to this array?

I tried the following:

decimal[] data = new decimal[]{};
data.Append(1011.11);

This returns the following error:

decimal[] does not contain a definition for Append

Ultimately, I'm trying to pass an array of decimal values to Chart.js via Anonymous Types and the array will be formed dynamically hence the use of Append rather than literal values:

Datasets = new[]
{
    new {
        label = "My First dataset",
        backgroundColor = "rgba(255, 99, 132, 0.5)",
        borderColor = "rgb(255, 99, 132)",
        data = data,
        fill = true,
    },
}

CodePudding user response:

There is no Append method for arrays, you can resize it and add the item like

decimal[] data = new decimal[] {};
Array.Resize(ref data, data.Length   1);
data[data.Length - 1] = 1011.11m;

Or alternate way is to use List<decimal> in place of the array here.

like this.

List<decimal> data = new List<decimal>();
data.Add(1011.11m);
  • Related