I think that the answer to "Why enums require an explicit cast to int type?" is that strongly-typed enums as per C# are a useful defence against common programming errors.
Imagine I had a painting management application, based on two enums:
enum Textures { Gloss, Satin, Matt };
enum Colors { White, Green, Red, Blue, Brown };
And a room-painting method like this:
private void PaintRoom (Textures texture, Colors color)
In a language with strong enum typing like in C#, a command like this:
// nonsensical attempt to have texture=White and color=Matt
PaintRoom(Colors.White, Textures.Matt);
...would yeild a compile-time error (can't put color into where a texture is expected and vice versa). But in languages where the enums aren't strongly-typed and/or implicit conversion can occur (including C and C++), both our Colors
and our Textures
can be treated as int
, so we can happily issue a PaintRoom(Colors.White, Textures.Matt)
command and it will compile fine, but we end up with a room painted Gloss Red (0, 2) instead of the Matt White (2, 0) that we intended and the code on a quick glance seems to say.
So strongly-typed enums are mostly a good thing, to stop people accidentally putting enums into places they were not meant for. And where someone really wants their enum as an int
, C# forces the coder to make their intention clear by insisting on an explicit conversion.