Complete Language Solution
Monday, August 9, 2010
Wednesday, June 23, 2010
C# Introduction
Introduction
C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in theSystem
namespace, as described in the table below Fully qualified name | Alias | Value range |
---|---|---|
System.Boolean | bool | true or false |
System.Byte | byte | 0 to 255 |
System.SByte | sbyte | -128 to 127 |
System.Char | char | 0 to 65535 |
System.Decimal | decimal | -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 |
System.Double | double | -1.79769313486232e308 to 1.79769313486232e308 |
System.Single | float | -3.40282347E+38 to 3.40282347E+38 |
System.Int16 | short | -32768 to 32767 |
System.Uint16 | ushort | 0 to 65535 |
System.Int32 | int | -2,147,483,648 to 2,147,483,647 |
System.UInt32 | uint | 0 to 4,294,967,295 |
System.Int64 | long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
System.UInt64 | ulong | 0 to 18,446,744,073,709,551,615 |
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 ValidShapeColorEnumerations are good code-documenting tools. For example, it is more intuitive to write the following:
{
Red = 0, Blue = 1, Yellow = 2
}
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.