3

I have following array:

double[] Series = new double[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 }

Series debugging view :

> Series
--> [0]
--> [1]
--> ...
--> [10]
--> [11]

When I write following:

object[] object_array = new object[] { Series }

object_array debugging view : (more than one level)

> object_array
--> [0]
----> [0]
----> [1]
----> ...
----> [10]
----> [11]

I write following to prevent new level:

object[] object_array = new object[Series.Length];
for (int i = 0; i < Series.Length; i++)
{
    object_array[i] = Series[i];
}

This is the one of the other solutions. But I think, there may be a better way to do this. Is there a problem for me to use above loop? Or different way?

(I use highcharts. If I give array that contains more than one level, it does not work.)

Thanks.

1
  • Seems like it will still pass a double array if you pass object_array[0]. Commented Nov 13, 2012 at 13:14

4 Answers 4

8
var object_array = Series.Cast<object>().ToArray();
Sign up to request clarification or add additional context in comments.

Comments

5

By doing this

object[] object_array = new object[] { Series }

you are assigning a double array as the first object item of the array object_array. You are actually creating 2D array with one row. You can use something like this:

double[] Series = new double[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 };
object[] object_array = new object[Series.Length];
Series.CopyTo(object_array, 0);

1 Comment

@ErenErsönmez, I m sorry, I did syntax error. Your code is true, too.
4

Use LINQ:

double[] series = new double[] { 7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 };
object[] seriesAsAbjectArray = series.Cast<object>().ToArray();

The reason why you cannot just cast it to an object[], is that double is a value type, and therefore you have to create a new array containing the boxed double-items.

Comments

-2

You just assign it, because double is an object.

object[] object_array = Series;

2 Comments

I get an error when I try that: 'Cannot implicitly convert type 'double[]' to 'object[]''
This just hides the fact that it's a double array, instead of changing it to an object array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.