Is it possible to cast int array into double array in immediate window? I tried to cast but somehow its not working. I would like to know that is it possible or not?
Asked
Active
Viewed 1.7k times
2 Answers
25
That cast is illegal. Just try to compile it and you will see that it doesn't work either.
The following code will perform this conversion:
var d = i.Select(x => (double)x).ToArray();
Unfortunately, you can't use it in the immediate window because it doesn't support lambda expressions.
A solution that doesn't require lambda expressions is the following:
i.Select(Convert.ToDouble).ToArray();
This could work because there is no lambda expression. Thanks to Chris for the idea.

Daniel Hilgarth
- 171,043
- 40
- 335
- 443
-
5Would using `Convert.ToDouble` instead of the Lambda do the trick? – Chris Feb 19 '14 at 14:16
-
1@Chris: You mean as in `i.Select(Convert.ToDouble).ToArray()`? Yes, that could very well work. – Daniel Hilgarth Feb 19 '14 at 14:17
-
1That is what I was thinking, yes. I've not got stuff open to test and I wasn't sure how up to the overload resolution it is when passing methods like that. – Chris Feb 19 '14 at 14:20
-
Well that's 50 rep I missed out on. ;-) Glad it worked though. For what its worth the thought process was "Lambdas aren't allowed. You could do it if you had a method already but then you'd have to write one and it takes away the point. Wait, what if there already was a method to convert between types?" – Chris Feb 19 '14 at 14:25
-
Of course this solution creates an array with a binary appearance that is very different from the original. If you wanted the same bits (but completely different numerical/mathematical meaning), you would need an array of type `long[]` instead, and then use `longArr.Select(BitConverter.Int64BitsToDouble).ToArray()`. – Jeppe Stig Nielsen Feb 19 '14 at 14:30
10
One more way is to use Array.ConvertAll
Array.ConvertAll<int, double>(nums, x => x);

volody
- 6,946
- 3
- 42
- 54
-
Love this suggestion! Should be faster than the LINQ solution given above. – JamesHoux Aug 03 '19 at 02:30