v C#’s
Data Types
Ø C#’s Value Types
→
For
a value type, a variable holds an actual value, such13.13.
→
At
the core of C# are the 13 value types shown in Table 3-1. Collectively, these
are referred to as the simple types. They are called simple types because they
consist of a single value. The simple types are also sometimes referred to as
primitive types.
→
NOTE In addition
to the simple types, C# defines three other categories of value types. These
are enumerations, structures, and null able types, all of which are described
later in this book.
§ Integers
→
C#
defines nine integer types: char, byte, sbyte, short, ushort, int, uint, long,
and ulong. However, the char type is primarily used for representing characters.
→
The
remaining eight integer types are used for numeric calculations. Their
bit-width and ranges are shown here:
Type
|
Width in bits
|
Range
|
Default value
|
Bool
|
8
|
True or False
|
False
|
Byte
|
8
|
0 to 255
|
0
|
Sbyte
|
8
|
-128 to 127
|
0
|
Char
|
16
|
0 to 128
|
NULL
|
Short
|
16
|
-32,768 to
32,767
|
0
|
Ushort
|
16
|
0 to 65,535
|
0
|
Int
|
32
|
-2,147,483,648
to 2,147,483,647
|
0
|
Uint
|
32
|
0 to
4,294,967,295
|
0
|
Long
|
64
|
-
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
|
0
|
Ulong
|
64
|
0 to
18,446,744,073,709,551,615
|
0
|
Float
|
32
|
±1.5 × 10-45
to ±3.4 × 1038
|
0
|
Double
|
64
|
±5.0 × 10-324
to ±1.7 × 10308
|
0
|
Decimal
|
128
|
± 1.0 × 10-28
to ± 7.9 × 1028
|
0
|
→
C#
defines both signed and unsigned versions of the various integer types. The
most commonly used integer type is int. If the value you want to store is
unsigned, you can use uint. For large signed values, use long. For large
unsigned values, use ulong.
→
The smallest integer types are byte and sbyte.
Variables of type byte are especially useful when working with raw binary data,
such as a byte stream produced by some device. For small signed integers, use
sbyte. When you need an integer that is larger than a byte or sbyte, but
smaller than an int or uint, use short or ushort.
§ Floating-Point Types
→
The
floating-point types can represent numbers that have fractional components.
There are two kinds of floating-point types, float and double.
→
The
float data type is for smaller floating - point values, for which less
precision is required. The double data type is bulkier than the float data type
but offers twice the precision (15 digits).
§ Decimal Type
→
The
decimal type represents higher - precision floating - point numbers. One of the
great things about the CTS and C# is the provision of a dedicated decimal type
for financial calculations.
0 comments:
Post a Comment