Understanding the Common Type System
The CTS sets forth the guidelines for data type safety in .NET.
In the past, there were no rules for type safety across execution runtimes, hence the general protection fault (GPF) and blue screen of death errors that could occur when running applications. The culprit behind those meltdowns was the overlapping of memory by data types. This was a common occurrence in Windows 3.1, Windows 95, and Windows 98. When a Visual Basic developer deployed a new application, fingers had to be crossed to make sure that the data types and memory access between the newly installed DLLs and the existing ones on the system mingled happily. Most of the time they did, but when they didn''t, errors occurred. |
Value Types
Value types directly contain the data you assign them. They''re built into the common language runtime and derive directly from the base System.Object type. Examples of value types are primitive types, structures, and enumerations. Primitive types can be further broken down into numbers, such as Boolean, byte, short, integer, long, single, double, decimal, date, and char.
Reference Types
Reference types don''t directly contain any data; rather, they point to a memory location that contains the actual data. Reference types are built into the common language runtime and derive directly from the base System.Object type. Some examples of reference types are strings, classes, arrays, delegates, and modules (see Figure 1.6).
Figure 1.6. The common type system defined.

To make the difference between Value types and Reference types clearer, consider the following code. It accesses a primitive type (which is a value type) and a class (which is a reference type), and attempts to assign values to them.
using System;
namespace cSharp_ValueReference
{
class Class1
{
static public int x;
[STAThread]
static void Main(string[] args)
{
x=4;
int y;
y = x;
x=0;
// Since each Value type contains its own data,
// modifying the variable X after setting Y to the value
// of X does not affect either variable
Console.WriteLine(x);
Console.WriteLine(y);
// Create an instance of Class2
Class2 ref1 = new Class2();
// Set the refValue of this instance to 5
ref1.refValue=5;
// Create an object reference to the ref1 class
Class2 ref2 = ref1;
// Set the refValue of the object
ref2.refValue=10;
// Notice how the results are the same, even
// though you set re1.refValue to 5, the reference
// to this memory was overridden by the value of 10
Console.WriteLine(ref1.refValue);
Console.WriteLine(ref2.refValue);
Console.ReadLine();
}
}
class Class2
{
public int refValue;
}
}
Module Module1
Sub Main()
Dim X As Integer = 4
Dim Y As Integer
intY = X
intX = 0
Console.WriteLine(X)
Console.WriteLine(Y)
Dim ref1 As Class2 = New Class2()
ref1.refValue = 5
Dim ref2 As Class2 = ref1
ref2.refValue = 10
Console.WriteLine(ref1.refValue)
Console.WriteLine(ref2.refValue)
Console.ReadLine()
End Sub
End Module
Class Class2
Public refValue As Integer
End Class
In both examples, the values of the value type variables X and Y are 0 and 4, whereas the values of the reference types ref1 and ref2 are both 10. Because the reference type points to the same memory allocation for the initial object ref1, the value for all variables set to an instance of that object is always the last value assigned. Figure 1.7 shows the console output of the code.
Figure 1.7. Value and reference type test output.

• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.