Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement an unknown Array type in C# .Net Core?

I have the following code below but haven't figured out how to implement the var results = section so that result[c] = valuesArray; works properly?

    var result = Array.CreateInstance(typeof(Array), dt.Columns.Count);

            foreach (int c in Enumerable.Range(0, dt.Columns.Count))
            {
                // Create a list to hold values of the required type for the column
                var valuesArray = Array.CreateInstance(typeof(float), dt.Rows.Count);

                // Get the data to be written to the parquet stream
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    DataRow row = dt.Rows[r];

                    valuesArray.SetValue(row[c], r);
                }

                result[c] = valuesArray;

The error is:

Error: CS0021 = Cannot apply indexing with [] to an expression of type 'Array'

How do I instiate the Array so it works?

like image 629
cdub Avatar asked Sep 08 '25 00:09

cdub


1 Answers

Just as you set a value to valuesArray with the SetValue() method, you can do the same for result:

result.SetValue(valuesArray, c);

If you need to retrieve the value during iteration, you can use result.GetValue(c):

foreach (int c in result)
{
    var retrievedResult = result.GetValue(c);
}
like image 53
David L Avatar answered Sep 10 '25 17:09

David L