1

I was trying to create an enumerable containing a series of doubles from 0 to X. My first attempt was:

Enumerable.Range(0, 10).Cast<double>();

But this gives the error

Unable to cast object of type 'System.Int32' to type 'System.Double'.

If I can't cast int32 to double, then why does the following work?:

Enumerable.Range(0, 10).Select(i => (double) i);
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
Loocid
  • 6,112
  • 1
  • 24
  • 42
  • 1
    Do these answer your question? [Explicit/implicit cast operator fails when using LINQ's .Cast() operator](https://stackoverflow.com/q/13316718/3744182) and [Why does a Linq Cast operation fail when I have an implicit cast defined?](https://stackoverflow.com/q/808725/3744182). – dbc Jan 20 '20 at 06:14
  • Does this answer your question? [Why does a Linq Cast operation fail when I have an implicit cast defined?](https://stackoverflow.com/questions/808725/why-does-a-linq-castt-operation-fail-when-i-have-an-implicit-cast-defined) – Nguyễn Văn Phong Jan 20 '20 at 06:15
  • 1
    Thanks guys, didn't see those. They answer my question. – Loocid Jan 20 '20 at 06:16

0 Answers0