The following code causes CS0266 in Visual Studio:
double x = 1.23;
int y = x;
But the following code compiles in Visual Studio, and causes an implicit cast double to int:
double x = 0;
ReadOnlyCollection<double> y = new ReadOnlyCollection<double>(new double[3] { 1.23, 2.34, 3.45 });
foreach (int z in y)
{
x += z;
}
Why is this treated differently? Can I cause compilation to fail?
I expect that an implicit cast to int when looping over an IEnumerable, would cause the same error as when casting a double to an int.
int
, usevar
and see the result. – Disentangle