I have an arraylist of doubles returned by a JSON library. After the JSON parser’s decode method is run, we have this in the C# locals window:
Name Value Type
myObj Count=4 object {System.Collections.ArrayList}
[0] 100.0 object {double}
[1] 244.0 object {double}
[2] 123.0 object {double}
[3] 999.0 object {double}
My goal is to produce an array of integers from this ArrayList. It would be simple to iterate and do this one value at a time, but I’d like to know how to do it using the built-in converter functionality. I have been reading theads on ConvertAll but I cannot get it to work.
I do not have control of the JSON library so I must begin with the ArrayList.
Thanks
You need to be careful with
ArrayLists because of boxing. Thus:Note the cast is framed as
(int)(double). This first unboxes the boxeddoubleand then casts to anint.To do this in older versions of .NET
An alternative is
Here we do not need an unboxing operation because we have first converted the
ArrayListto an array of unboxeddoubles.To do this in older versions of .NET