Why Some Types Lack Literal Modifiers
In C#, integral types like long int have literal modifiers, while others like short int do not. This distinction raises the question of why certain types have this feature and not others.
The principle behind C#'s design is that features are absent by default and must be justified by their benefits. In the case of literal modifiers, their benefits apply to types like long and unsigned types because these enable precise specification of the intended integer size or signedness.
Justification for Modifiers on long and Unsigned Types
The vast majority of integer calculations involve 32-bit signed integers, which are efficiently handled by hardware. C# defaults to this representation. However, for calculations requiring larger ranges, long integers are used, and a modifier allows for clear indication of this intended size.
Moreover, interop scenarios and bit field manipulation require specifying unsigned integers. Again, modifiers provide a concise method to indicate the intended signedness or lack thereof.
Lack of Justification for Modifiers on short
In contrast, short integers do not require a modifier because:
The benefits of modifiers do not extend to short types. Since arithmetic is not done in shorts and literals can already be used with shorts, there is no compelling justification for introducing a short modifier.
The above is the detailed content of Why Don't `short` Integers Have Literal Modifiers in C#?. For more information, please follow other related articles on the PHP Chinese website!