Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
ch02

ch02

Ratings: (0)|Views: 22|Likes:
Published by api-3779640

More info:

Published by: api-3779640 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

2
CHAPTER
Variables and
Program Structure

In this chapter, you will
\ue000Learn more about value-type and reference-type variables
\u2022See the uses of boxing and unboxing
\u2022See how objects are created and destroyed
\u2022Learn the function of the garbage collector
\u2022Discover the structure of a C# class file
\u2022Get acquainted with the syntax of the C# programming language
\u2022Learn to recognize keywords, operators, statements and blocks
\u2022 Learn to control program flow with conditional expressions and loops
\u2022 Get to understand theusing directive and namespaces

Microsoft\u2019s .NET Framework defines the Common Type System (CTS) that specifies the universal data types used by all languages in the .NET family. The CTS defines both the data types andthe rules for how the CLR declares and uses these data types. In this chap- ter, we will explore the CTS and the use of the different data types in C# programming, as well as the two categories of types\u2014value types and reference types. We\u2019ll also look at the process of converting between value and reference types by boxing and unboxing. Finally we will cover how objects are constructed on the heap (the heap of memory), and how they are removed (destroyed) by the garbage collector. We will also examine the structure of class files by looking at where you should declare variables and meth- ods, how you should use keywords and operators, how you can control the program flow, and so on.

The Microsoft exams will assume that you have a solid foundation for the informa- tion that this chapter presents. Take your time to ensure that you are comfortable with the elements in this chapter\u2014you will more than likely not be tested on them directly, but you will need to be very fluent in their usage.

1
Variables

The C# language defines two types of variables that you will use for data and objects; variables can be value-type and reference-type variables. In the following sections you will explore the variable types as well as the data types they are used with.

Value-Type Variables

When you need to store numbers or characters in a program, you will most likely use value-type variables. While you could create a class for the data, we will assume for now that we are only using the basic data types.

Value-type variables are based on the primitive data types defined in the CLR. These primitive data types will always be the same on all platforms. In Table 2-1 you can see the mapping between C# and the primitive data types.

The value types are divided into integral, floating, decimal, and Boolean types. You
will see the specifics for these value types in the following sections.
Integral Types

Integral types represent a whole number (for example, 42 and 491) that can be either signed or unsigned. A signed type can represent both positive and negative numbers, while an unsigned type can only represent positive numbers.

The C# language defines nine integral types: sbyte, byte, short, ushort, int, uint, long,
ulong, and char. In Table 2-2 the ranges of values for the integral types are listed.
MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide
2
C# Variable Type
CLR Data Type
Description
sbyte
System.Sbyte
8-bit signed integer
short
System.Int16
16-bit signed integer
int
System.Int32
32-bit signed integer
long
System.Int64
64-bit signed integer
byte
System.Byte
8-bit unsigned integer
ushort
System.UInt16
16-bit unsigned integer
uint
System.UInt32
32-bit unsigned integer
ulong
System.UInt64
64-bit unsigned integer
float
System.Single
Single-precision floating-point value
double
System.Double
Double-precision floating-point value
char
System.Char
Unicode character
decimal
System.Decimal
Exact decimal with 28 significant digits
bool
System.Boolean
Boolean value
Table 2-1
The C# Versus Primitive Data Types
Chapter 2: Variables and Program Structure
3
PARTI
NOTEThe Unicode character set represents all possible characters in one
double-byte character. Unicode is the standard for national language support;
C# uses only Unicode characters.
Floating-Point Types

Floating-point types represent signed numbers with decimal portions (for example, 3.14159 and 42.12). The C# language supports two floating-point types: float and double. Thefloat type is represented using a 32-bit precision and thedouble using a 64-bit precision IEEE 754 format. Thedouble type can represent values from ap- proximately

5.0 \u00d7 10-324
to
1.7 \u00d7 10308
(with a precision of 15 to 16 digits)
while thefloat can represent values from approximately
1.5 \u00d7 10-45
to
3.4 \u00d7 1038
(with a precision of 7 digits)
Decimal Types
The decimal type is represented by a 128-bit (16-byte) data type that is designed for cur-
rency or financial calculations. The decimal type can represent signed values from
1.0 \u00d7 10-28
to about
7.9 \u00d7 1028
(with 28 to 29 significant digits)
Calculations using the decimal type are exact to 28 or 29 digits, but never to more than
28 decimal places.
Type
Value Range
sbyte
Signed 8-bit integer between \u2013129 and 127
byte
Unsigned 8-bit integer between 0 and 255
short
Signed 16-bit integer between \u201332,768 and 32,767
ushort
Unsigned 16-bit integer between 0 and 65,535
int
Signed 32-bit integer between \u20132,147,483,648 and 2,147,483,647
uint
Unsigned 32-bit integer between 0 and 4,294,967,295
long
Signed 64-bit integer between \u20139,223,372,036,854,775,808 and
9,223,372,036,854,775,807
ulong
Unsigned 64-bit integer between 0 and 18,446,744,073,709,551,615
char
Unsigned 16-bit integer between 0 and 65,535 representing the Unicode characters
Table 2-2
The Integral Data Types

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->