English 中文(简体)
Why won t Cast<double>() work on IEnumerable<int>? [duplicate]
原标题:

This question already has answers here:

Closed 13 years ago.

Possible Duplicates:
Enumerable.Cast<T> extension method fails to cast from int to long, why ?
Puzzling Enumerable.Cast InvalidCastException
Cast/Convert IEnumerable<T> to IEnumerable<U> ?

I m trying to convert an array of integers to an array of doubles (so I can pass it to a function that takes an array of doubles).

The most obvious solution (to me, at least) is to use the Cast extension function for IEnumerable, but it gives me an InvalidCastException, and I don t understand why. My workaround is to use Select instead, but I think Cast looks neater.

Could someone tell me why the Cast method isn t working?

Hopefully the code below illustrates my problem:

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    class Program
    {
        static void Main()
        {
            var intArray = new[] { 1, 2, 3, 4 };
            PrintEnumerable(intArray, "intArray: ");

            var doubleArrayWorks = intArray.Select(x => (double)x).ToArray();
            PrintEnumerable(doubleArrayWorks, "doubleArrayWorks: ");

            // Why does this fail??
            var doubleArrayDoesntWork = intArray.Cast<double>().ToArray();
            PrintEnumerable(doubleArrayDoesntWork, "doubleArrayDoesntWork: ");

            // Pause
            Console.ReadLine();
        }

        private static void PrintEnumerable<T>(
            IEnumerable<T> toBePrinted, string msgPrefix)
        {
            Console.WriteLine(
                msgPrefix + string.Join(
                    ",", toBePrinted.Select(x => x.ToString()).ToArray()));
        }
    }

}

问题回答

The problem comes from the fact that cast operators (overloaded) are resolved at compile time. Try to think how Cast is implemented. I bet the code looks like this:

public static IEnumerable<T> Cast<T>(this IEnumerable source)
{
   foreach(object element in source)
   {
      yield return (T)(object)element;
   }
}

All the info the compiler has is that it needs to cast an object to a type T. And for that it will use the default inheritance casting. No custom overloaded operator will be used. And in your example an int is not a double so the cast will fail.

The select example:

source.Select(a => (double)a));

works because the compiler knows both types and it s able to call the appropriate overloaded operator.

Try using the Convertall method of the Array class. It gives you explicit control over the conversion.

var doubleArray = Array.ConvertAll<int, double>(intArray, num => (double)num);

This bypasses the internal error that you are experiencing.

Other methods give you explicit control over the conversion process too.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签