Simple types are value types that are a subset of the built-in types in C#, although, in fact, the types are defined as part of the .NET Framework Class Library (.NET FCL). Simple types are made up of several numeric types and a bool type. These numeric types consist of a decimal type (decimal), nine integral types (byte, char, int, long, sbyte, short, uint, ulong, ushort), and two floating-point types (float, double). Table 1-1 lists the simple types and their fully qualified names in the .NET Framework.
The C# reserved words for the various data types are simply aliases for the fully qualified type name. Therefore, it does not matter whether you use the type name or the reserved word: the C# compiler will generate identical code. It should be noted that the following types are not Common Language Specification-compliant (CLS-compliant): sbyte, ushort, uint, and ulong. They might not be supported by other .NET languages as a result of this. This lack of support might limit or impede the interaction between your C# code and code written in another CLS-compliant language, such as Visual Basic .NET. Enumerations implicitly inherit from System.Enum, which in turn inherits from System.ValueType. Enumerations have a single use: to describe items of a specific group. For example, the colors red, blue, and yellow could be defined by the enumeration ValidShapeColor; likewise, square, circle, and triangle could be defined by the enumeration ValidShape. These enumerations would look like the following: enum ValidShapeColor { Red, Blue, Yellow } enum ValidShape { Square = 2, Circle = 4, Triangle = 6 } Each item in the enumeration receives a numeric value regardless of whether you assign one or not. Since the compiler automatically adds the numbers starting with zero and incrementing by one, for each item in the enumeration, the ValidShapeColor enumeration previously defined would be exactly the same if it were defined in the following manner: enum ValidShapeColor { Red = 0, Blue = 1, Yellow = 2 } Enumerations are good code-documenting tools. For example, it is more intuitive to write the following: ValidShapeColor currentColor = ValidShapeColor.Red; instead of this: int currentColor = 0; Either mechanism will work, but the first method is easy to read and understand, especially for a new developer taking over someone else's code. It also has the benefit of being type-safe, which the use of raw ints does not provide. |