This chapter discusses the data types you will use in Visual Studio C# and their declarations.
A Common Language Specification (CLS) now exists for C#, a specification initially written by Microsoft but approved by most international computer committees . The following table shows the CLS-compliant data types in Visual Studio C#:
byte | 0 minimum, 255 maximum; described in System.Byte. |
short | - 32,768 minimum, 32,767 maximum; described in System.Int16. |
int | - 2,147,483,648 minimum, 2,147,483,647 maximum; described in System.Int32. |
long | - 9,223,372,036,854,775,808 minimum, 9,223,372,036,854,775,807 maximum; described in System.Int64. |
string | 0 minimum, no maximum; described in System.String. |
char | 0 minimum, no maximum; described in System.Char. One byte (8 bits) if ASCII, two bytes (16 bits) if Unicode (which provides for characters in languages such as Japanese). |
bool | Either true or false; described in System.Bool. |
float | 32 bits ” 23 bits for the number (7 digits), 8 bits for the exponent, and 1 sign bit; described in System.Single. |
double | 64 bits ” 52 bits for the number (15 to 16 digits), 11 bits for the exponent, and 1 sign bit; described in System.Double. |
decimal | 102 bits ” 96 bits for an integer number, 5 bits for a scaling factor, and 1 sign bit. For example, the integer number 1234567 with a scaling factor of 3 becomes 122334.567; described in System.Decimal. |
Note | Since a decimal point may appear in both the decimal data type and the float/double data types, it may be necessary to mark a number with either an m for decimal (like the first letter in money ) or an f for float. If a number is unmarked (and has a decimal point in it), the compiler will assume that the number is a double. |
Here are some examples:
decimal decDollars = 101.67m;
If the letter m is removed from the end of 101.67, the compiler assumes the number is a double. Then, to place the number into a decimal named decDollars, the number is converted from a double to a decimal. With the m affixed to the number 101.67, the compiler knows that the number is a decimal.
double dblDollars = 101.7f;
The f at the end of the number confirms to the compiler that this is indeed a float number. However, if you remove the f (which stands for floating point), the compiler assumes that the number is a double anyway.
Since the double is the default, we recommend that you perform all floating-point arithmetic using double.
You will probably do 99.9 percent of your C# work using three data types: int, double, and string. If you are a finance person you will probably add a fourth, decimal.