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.
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.
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.
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.
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
This action might not be possible to undo. Are you sure you want to continue?