Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert int[] to double[] using Cast<T>? [duplicate]

Possible Duplicate:
IEnumerable.Cast<>

One can implicitly convert from int to double. Why does "Specified cast is not valid." exception is raised here?

 double[] a = Enumerable.Range(0, 7).Cast<double>().ToArray();

I have tried several "versions" of it.

P.S. I know possible solutions like:

 double[] a = Enumerable.Range(0, 7).Select(x => (double)x).ToArray();

But I'm curious how Cast works => why it doesn't work in this example which looks so obvious.

like image 733
MajesticRa Avatar asked Mar 20 '12 21:03

MajesticRa


2 Answers

Cast is made to turn an IEnumerable (untyped) to an IEnumerable<T> (generically typed). It won't actually differently-cast any of the members.

Per this answer:

Well, you have incorrect expectations of Cast, that's all - it's meant to deal with boxing/unboxing, reference and identity conversions, and that's all. It's unfortunate that the documentation isn't as clear as it might be

So, you're stuck with .Select().

like image 200
eouw0o83hf Avatar answered Oct 16 '22 05:10

eouw0o83hf


The reason this fails is because essentially you're doing this:

        int x = 10;
        object f = x;
        double d = (double) f;

The int is getting boxed into the object, and when you go to unbox it, you're trying to unbox it to a double.

More specifically, here's the implementation of Cast:

public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
{
    IEnumerable<TResult> enumerable = source as IEnumerable<TResult>;
    if (enumerable != null)
    {
        return enumerable;
    }
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    return Enumerable.CastIterator<TResult>(source);
}

private static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source)
{
    foreach (object current in source)
    {
        yield return (TResult)current;
    }
    yield break;
}

As you can see, it's looping through the IEnumerable and boxing each element in the source. It then tries to unbox at which point you blow up.

like image 35
BFree Avatar answered Oct 16 '22 06:10

BFree