This raises a number of questions.
Why can't I declare an enum inheriting from Byte, but can from a byte?
As others have noted, this is what the specification says.
The entries of the language design committee do not justify this decision. June 2000 notes
Keywords for predefined types (e.g., int) and their corresponding type names (e.g., Int32) can be used interchangeably, but not completely. We discussed whether we want to make any changes to this area. We did not.
Here is a list of places where they are not interchangeable.
- "int" can be used as the base type for enumerations; Int32 cannot.
- Nickname ads cannot use keywords.
Some of my thoughts on this issue:
The first thing that comes to mind is that every time you give the user a choice, you give them the opportunity to write an error, and every time you give them the opportunity to write an error, you must make an error message for this. If we allowed an "X: byte enumeration", then we give a reasonable error message when the user accidentally deleted "using System;". We can avoid this confusion and all the costs of developing and testing a heuristic that reports errors by simply not allowing a choice in the first place.
The second thing that comes to mind is that the main type of enumeration is fundamentally connected with the enumeration mechanism, and not with its meaning. Therefore, it seems plausible that a sentence of a basic type should be limited to things that do not require semantic analysis; we know that the base type is one of eight possible types, so just let the user mechanically select one of these eight types uniquely.
The third thing that comes to mind is that error analysis can be performed during parsing rather than semantic analysis. The sooner the error is caught, the better.
UPDATE: I just asked one of the people who were in the room that day if there was something that I missed in my thoughts, and he said yes, they thought about it and decided that a complete type analysis is this is work for a development team, testing and maintenance, work that did not buy the user anything of value. (He also noted that they had similar arguments regarding System.Void: should it be legal to say βpublic static System.Void Main (string [] args)β for example? Again, they decided that this did not add value to users, but add potential ambiguity and work for the team.)
Why is char not a legal base type?
Again, what the spec says. Again, notes on language design since October 1999 do not help determine why:
Unsigned integral types can be used as base types for enumerations. The only integral type that cannot be used is char.
Again, we can guess. I assume the enumerations are for fancy numbers. Balls in practice are integers as part of the implementation, but logically they are not numbers, they are symbols. We want to be able to do operations with enumerations, such as adding, or-ing and and-ing flags, etc .; The specification makes it clear that these operations are performed as if they were performed according to the base type. The char type, which is not a logical number, does not define all the operators you may need. And finally, if you want a double-byte enumeration value, then you already have short and convenient ones at your disposal.
Related question from a letter on this subject:
A careful reading of the grammar specification indicates that βcharβ is a grammatically legal base type, but a paragraph of explanatory text after the grammar indicates that βcharβ is not a legal base type. Is the specification inconsistent?
Yes. I do not lose sleep over him. If this makes you feel better, imagine a grammar line that says
enum-base: integral type
reads instead
enum-base: integral type (but not char )