Built-in Value Types

salmonbrisketΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

60 εμφανίσεις

Chapter


Frame
work Fundamentals

Page
1

of
3

Using Value Types


The simplest types in the .NET Framework, primarily numeric and Boolean types, are
value types. Value types are variables that contain their data directly instead of containing
a reference to the data stored elsewhere in memory. Instanc
es of value types are stored in
an area of memory called the
stack
, where the runtime can create, read, update, and
remove them quickly with minimal overhead.

There are three general value types:



Built
-
in types



User
-
defined types



Enumerations

Each of these types is derived f
rom the
System.Value

base type.

Built
-
in Value Types

Built
-
in types are base types provided with the .NET Framework, with which other types
are built. All built
-
in numeric types are value types. You choose a numeric type based on
the size of the values you

expect to work with and the level of precision you require.
Table 1
-
1

lists the most common numeric types by size, from smallest to largest. The first
six types are used for whole number values and the last three represent real numbers in
order of increasing precision
.

Table 1
-
1: Built
-
in Value Types

Type (Visual
Basic/C# alias)

Bytes

Range

Use for

System.SByte
(
SByte
/
sbyte
)

1

-
128 to 127

Signed byte values

System.Byte
(
Byte
/
byte
)

1

0 to 255

Unsigned bytes

System.Int16
(
Short
/
short
)

2

-
32768 to 32767

Interoper
ation and
other specialized
uses

System.Int32
(
Integer
/
int
)

4

-
2147483648 to 2147483647

Whole numbers
and counters

System.UInt32
(
UInteger
/
uint
)

4

0 to 4294967295

Positive whole
numbers and
counters

System.Int64
(
Long
/
long
)

8

-
9223372036854775808 to
922
3372036854775807

Large whole
numbers

Chapter


Frame
work Fundamentals

Page
2

of
3

Table 1
-
1: Built
-
in Value Types

Type (Visual
Basic/C# alias)

Bytes

Range

Use for

System.Single
(
Single
/
float
)

4

-
3.402823E+38 to 3.402823E+38

Floating point
numbers

System.Double
(
Double
/
double
)

8

-
1.79769313486232E+308 to
1.79769313486232E+308

Precise or large
floating point
numbers

System.Decim
al
(
Decimal
/
decimal
)

16

-
79228162514264337593543950335 to
79228162514264337593543950335

Financial and
scientific
calculations
requiring great
precision

Best Practices

Optimizing performance
with built
-
in types


The runtime optimizes the performance of 3
2
-
bit integer
types (
Int32

and
UInt32
), so use those types for counters and
other frequently accessed integral variables. For floating
-
point operations,
Double

is the most efficient type because
those operations are optimized by hardware.

These numeric ty
pes are used so frequently that Visual Basic and C# define aliases for
them. Using the alias is equivalent to using the full type name, so most programmers use
the shorter aliases. In addition to the numeric types, the non
-
numeric data types listed in
Table 1
-
2

are als
o value types.

Table 1
-
2: Other Value Types

Type (Visual
Basic/C# alias)

Bytes

Range

Use for

System.Char
(
Char
/
char
)

2

N/A

Single Unicode
characters

System.Boolean
(
Boolean
/
bool
)

4

N/A

True
/
False

values

System.IntPtr

(none)

Platform
-
dependent

N/A

Pointer to a
memory address

System.DateTime
(
Date
/
date
)

8

1/1/0001 12:00:00 AM to
12/31/9999 11:59:59 PM

Moments in time

There are nearly 300 more value types in the Framework, but the types shown here cover
most needs. When you assign between value
-
type

variables, the data is copied from one
variable to the other and stored in two different locations on the stack. This behavior is
different from that of reference types, which are discussed in
Lesson 2
.

Even though value types often represent simple values, they still functio
n as objects. In
other words, you can call methods on them. In fact, it is common to use the
ToString

Chapter


Frame
work Fundamentals

Page
3

of
3

method when displaying values as text.
ToString

is overridden from the fundamental
System.Object

type.

The
Object

base class


In the .NET Framework, all
types are derived from
System.Object
. That relationship helps establish the common
type system used throughout the Framework.

How to Declare Value Types

To use a type, you must first declare a symbol as an instance of that type. Value types
have an implic
it constructor, so declaring them instantiates the type automatically; you
don't have to include the
New

keyword as you do with classes. The constructor assigns a
default value (usually
null

or
0
) to the new instance, but you should always explicitly
initi
alize the variable within the declaration, as shown in the following code block:


Keyword differences in
Visual Basic and C#


One of the cosmetic differences between Visual Basic and
C# is that Visual Basic capitalizes keywords, whereas C#
uses lowercase k
eywords. In the text of this book,
keywords will always be capitalized for readability. Code
samples will always include separate examples for Visual
Basic and C#.

' VB

Dim b As Boolean = False


// C#

bool b = false;


Variable capitalizations in
Visual B
asic and C#


C# is case
-
sensitive, but Visual Basic is not case
-
sensitive.
Traditionally, variable names begin with a lowercase letter
in C# and are capitalized in Visual Basic. For consistency
between the languages, this book will use lowercase
variable n
ames for most Visual Basic examples. Feel free
to capitalize Visual Basic variables in your own code

it
will n潴 affect h潷 the runtime 灲潣esses y潵r c潤o.

Declare the varia扬e as
nullable

if you want to be able to determine whether a value has
not been
assigned. For example, if you are storing data from a yes/no question on a form
and the user did not answer the question, you should store a
null

value. The following
code allows a Boolean variable to be
true
,
false
, or
other
: