Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Construct array of decimals dynamically

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,
    },
}

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading