按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
you spend 8; it does not affect the 10 that your spouse has; as befits the value type model。
However; if you and your spouse have 10 available with your credit card and you spend 8;
only 2 remain; as you would expect with a reference type。
…………………………………………………………Page 65……………………………………………………………
CH A PT E R 2 ■ L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 43
The variable total is a value type
and stored on the stack
Stack
1:total = 3
! 〃
# % & '
()* 〃 + &*
; #
; Method call
Stack
empty
There are no parameters
…。/ and no local variables;
; thus the stack is empty
Each called function has a stack
;
that contains the function
arguments and variables declared
in the function
Figure 2…14。 Stacks that are created and the interaction with the heap during CLR execution
There are times when you use value types and times when you use reference types; just as
there are times when you pay for things using cash and times when you use a credit card。 Typi
cally though; you use credit cards when you want to pay for expensive things; because you
don’t want to carry around large amounts of cash。 This applies to value and reference types; in
that you don’t want to keep large footprint value types on the stack。
By knowing the difference between the stack and heap; you automatically know the differ
ence between a value type and a reference type; as they are directly related。 Value types are
stored on the stack; and the contents of reference types are stored on the heap。
Understanding the CLR Numeric Types
The CLR has two major types of numbers: whole numbers and fractional numbers。 Both of
these number types are value…based data types; as explained in the previous section。 The Add()
method used the type Integer; which is a whole number–based value type。 As you saw; whole
numbers have upper limits; which are set by the space available。
Consider the following number:
123456
This number takes six spaces of room。 For illustrative purposes; imagine that the page you
are reading allows only six spaces of room for numerals。 Based on that information; the largest
number that can be written on this page is 999;999; and the smallest is 0。 In a similar manner;
specific number types force the CLR to impose restrictions on how many spaces can be used to
represent a number。 Each space is a 1 or a 0; allowing the CLR to represent numbers in binary
notation。
…………………………………………………………Page 66……………………………………………………………
44 CH AP T E R 2 ■ L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
puters may use binary notations; but humans work better with decimals; so to calcu
late the largest possible number a data type can store; you use 2 to the power of the number of
spaces and then subtract 1。 In the case of the Integer type; there are 32 spaces。 Before we calculate
the biggest number Integer can store; though; we need to consider negative numbers。 The
upper limit of Integer isn’t actually 4;294;967;295 (the result of 232 – 1); because Integer also
stores negative numbers。 In other words; it can save a negative whole number; such as –2。
The puter uses a trick in that the first space of the number is reserved for the sign (plus
or minus) of the number。 In the case of Integer; that means there are only 31 spaces for
numbers; so the largest number that can be represented is 2;147;483;647; and the smallest is
–2;147;483;648。 Going back to our addition example; this fact means that when the result of our
addition is 4 billion; which in binary requires 32 spaces; Integer does not have the space to store it。
The environment includes the numeric data types listed in Table 2…1; which have
varying sizes and storage capabilities。 The following terminology is used to describe numeric
data types:
o A bit is a space of storage; and 8 bits make a byte。
o Integers are whole numbers。
o Floating…point types are fractional numbers。
o Signed means one space in the number is reserved for the plus or negative sign。
Table 2…1。 Numeric Data Types
Type Description
Byte Unsigned 8…bit integer; the smallest value is 0; and the largest value is 255
SByte Signed 8…bit integer; the smallest value is –128; and the largest value is 127
UShort Unsigned 16…bit integer; the smallest value is 0; and the largest value is 65535
Shor