The Size and Range of C# Integral Types
Type |
Size (in bits) |
Range |
sbyte |
8 |
-128 to 127 |
byte |
8 |
0 to 255 |
short |
16 |
-32768 to 32767 |
ushort |
16 |
0 to 65535 |
int |
32 |
-2147483648 to 2147483647 |
uint |
32 |
0 to 4294967295 |
long |
64 |
-9223372036854775808 to 9223372036854775807 |
ulong |
64 |
0 to 18446744073709551615 |
char |
16 |
0 to 65535 |
Integral types are well suited for those operations involving
whole number calculations. The char type is the
exception, representing a single Unicode character. As you can see
from the table above, you have a wide range of options to choose
from, depending on your requirements.
Floating Point and Decimal Types.
Table 2-2. The Floating Point and Decimal Types with Size,
Precision, and Range
Type |
Size (in bits) |
Precision |
Range |
float |
32 |
7 digits |
1.5 x 10-45 to 3.4 x 1038 |
double |
64 |
15-16 digits |
5.0 x 10-324 to 1.7 x 10308 |
decimal |
128 |
28-29 decimal places |
1.0 x 10-28 to 7.9 x 1028 |
Floating point types are used when you need to perform
operations requiring fractional representations. However, for
financial calculations, the decimal type is the best
choice because you can avoid rounding errors.
The string Type
A string is a string of text characters. You typically create a
string with a string literal, enclosed in quotes: "This is an
example of a string." You've seen strings being used since Lesson
1, where we used the Console.WriteLine method to send
output to the console.
Some characters aren't printable, but you still need to use
them in strings. Therefore, C# has a special syntax where
characters can be escaped to represent non-printable characters.
For example, it is common to use newlines in text, which is
represented by the '\n' char. The backslash, '\', represents the
escape. When preceded by the escape character, the 'n' is no
longer interpreted as an alphabetical character, but now
represents a newline.
You may be now wondering how you could represent a backslash
character in your code. We have to escape that too by typing two
backslashes, as in '\\'. Table 2-3 shows a list of common escape
sequences.
Table 2-3. C# Character Escape Sequences
Escape Sequence |
Meaning |
\' |
Single Quote |
\" |
Double Quote |
\\ |
Backslash |
\0 |
Null, not the same as the C# null value |
\a |
Bell |
\b |
Backspace |
\f |
Form Feed |
\n |
Newline |
\r |
Carriage Return |
\t |
Horizontal Tab |
\v |
Vertical Tab |
Another useful feature of C# strings is the verbatim literal,
which is a string with a @ symbol prefix, as in @"Some string".
Verbatim literals make escape sequences translate as normal
characters to enhance readability. To appreciate the value of
verbatim literals, consider a path statement such as "c:\\topdir\\subdir\\subdir\\myapp.exe".
As you can see, the backslashes are escaped, causing the string to
be less readable. You can improve the string with a verbatim
literal, like this: @"c:\topdir\subdir\subdir\myapp.exe".
That is fine, but now you have the problem where quoting text
is not as easy. In that case, you would specify double double
quotes. For example, the string "copy \"c:\\source file name
with spaces.txt\" c:\\newfilename.txt" would be written as
the verbatim literal @"copy ""c:\source file name with
spaces.txt"" c:\newfilename.txt".
C# Operators
Results are computed by building expressions. These
expressions are built by combining variables and operators
together into statements. The following table describes the
allowable operators, their precedence, and associativity.
Table 2-4. Operators with their Precedence and Associativity
Category (by precedence) |
Operator(s) |
Associativity |
Primary |
(x) x.y f(x) a[x] x++ x-- new typeof
sizeof checked unchecked |
left |
Unary |
+ - ! ~ ++x --x (T)x |
left |
Multiplicative |
* / % |
left |
Additive |
+ - |
left |
Shift |
<< >> |
left |
Relational |
< > <= >= is |
left |
Equality |
== != |
right |
Logical AND |
& |
left |
Logical XOR |
^ |
left |
Logical OR |
| |
left |
Conditional AND |
&& |
left |
Conditional OR |
|| |
left |
Conditional> |
?: |
right |
Assignment |
= *= /= %= += -= <<= >>= &= ^= |= |
right |
Left associativity (mathematical terms) means that operations are evaluated from
left to right. Right associativity mean all operations occur from
right to left, such as assignment operators where everything to
the right is evaluated before the result is placed into the
variable on the left. |