Σχόλια 0

Το κείμενο του εγγράφου

Chapter–

Framework Fundamentals

Page1

of3

Using Value Types

The simplest types in the .NET Framework, primarily numeric and Boolean types, arevalue types. Value types are variables that contain their data directly instead of containinga reference to the data stored elsewhere in memory. Instances of value types are stored inan area of memory called thestack, where the runtime can create, read, update, andremove them quickly with minimal overhead.

There are three general value types:



Built-in types



User-defined types



Enumerations

Each of these types is derived from theSystem.Value

base type.

Built-in Value Types

Built-in types are base types provided with the .NET Framework, with which other typesare built. All built-in numeric types are value types. You choose a numeric type based onthe 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 firstsix types are used for whole number values and the last three represent real numbers inorder of increasing precision.

Table 1-1: Built-in Value Types

Type (VisualBasic/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

Interoperation andother specializeduses

System.Int32(Integer/int)

4

-2147483648 to 2147483647

Whole numbersand counters

System.UInt32(UInteger/uint)

4

0 to 4294967295

Positive wholenumbers andcounters

System.Int64(Long/long)

8

-9223372036854775808 to9223372036854775807

Large wholenumbers

Chapter–

Framework Fundamentals

Page2

of3

Table 1-1: Built-in Value Types

Type (VisualBasic/C# alias)

Bytes

Range

Use for

System.Single(Single/float)

4

-3.402823E+38 to 3.402823E+38

Floating pointnumbers

System.Double(Double/double)

8

-1.79769313486232E+308 to1.79769313486232E+308

Precise or largefloating pointnumbers

System.Decimal(Decimal/decimal)

16

-79228162514264337593543950335 to79228162514264337593543950335

Financial andscientificcalculationsrequiring greatprecision

Best Practices—Optimizing performancewith built-in types

The runtime optimizes the performance of 32-bit integertypes (Int32

andUInt32), so use those types for counters andother frequently accessed integral variables. For floating-point operations,Double

is the most efficient type becausethose operations are optimized by hardware.

These numeric types are used so frequently that Visual Basic and C# define aliases forthem. Using the alias is equivalent to using the full type name, so most programmers usethe shorter aliases. In addition to the numeric types, the non-numeric data types listed inTable 1-2

are also value types.

Table 1-2: Other Value Types

Type (VisualBasic/C# alias)

Bytes

Range

Use for

System.Char(Char/char)

2

N/A

Single Unicodecharacters

System.Boolean(Boolean/bool)

4

N/A

True/False

values

System.IntPtr

(none)

Platform-dependent

N/A

Pointer to amemory address

System.DateTime(Date/date)

8

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

Moments in time

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

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

Even though value types often represent simple values, they still function as objects. Inother words, you can call methods on them. In fact, it is common to use theToString

Chapter–

Framework Fundamentals

Page3

of3

method when displaying values as text.ToString

is overridden from the fundamentalSystem.Object

type.

TheObject

base class

In the .NET Framework, alltypes are derived fromSystem.Object. That relationship helps establish the commontype 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 typeshave an implicit constructor, so declaring them instantiates the type automatically; youdon't have to include theNew

keyword as you do with classes. The constructor assigns adefault value (usuallynull

or0) to the new instance, but you should always explicitlyinitialize the variable within the declaration, as shown in the following code block:

Keyword differences inVisual Basic and C#

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

' VB

Dim b As Boolean = False

// C#

bool b = false;

Variable capitalizations inVisual Basic and C#

C# is case-sensitive, but Visual Basic is not case-sensitive.Traditionally, variable names begin with a lowercase letterin C# and are capitalized in Visual Basic. For consistencybetween the languages, this book will use lowercasevariable names for most Visual Basic examples. Feel freeto capitalize Visual Basic variables in your own code—itwill n潴 affect h潷 the runtime 灲潣esses y潵r c潤o.

Declare the varia扬e asnullable

if you want to be able to determine whether a value hasnot beenassigned. For example, if you are storing data from a yes/no question on a formand the user did not answer the question, you should store anull

value. The followingcode allows a Boolean variable to betrue,false, orother: