Can anyone explain this?
alt text http://www.deviantsart.com/upload/g4knqc.png
using System;
namespace TestEnum2342394834
{
class Program
{
Enum.GetValues
is declared as returning Array
.
The array that it returns contains the actual values as ReportStatus
values.
Therefore, the var
keyword becomes object
, and the value
variable holds (boxed) typed enum values.
The Console.WriteLine
call resolves to the overload that takes an object
and calls ToString()
on the object, which, for enums, returns the name.
When you iterate over an int
, the compiler implicitly casts the values to int
, and the value
variable holds normal (and non-boxed) int
values.
Therefore, the Console.WriteLine
call resolves to the overload that takes an int
and prints it.
If you change int
to DateTime
(or any other type), it will still compile, but it will throw an InvalidCastException
at runtime.