Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

Understanding the Common Type System

The CTS sets forth the guidelines for data type safety in .NET.

In .NET, the CTS defines types and how they can act within the bounds of the common language runtime. There are two type classifications in .NET: value types and reference types.

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).

01fig06.gif

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.

c_icon.gif
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;
        }
}
vbnet_icon.gif
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.

01fig07.jpg

Figure 1.7 Value and reference type test output.

Now that you have an understanding of what the CTS is and how it works, you need to see how the types are removed from memory. Removing types that are no longer referenced in your applications is known as garbage collection.

Share ThisShare This

Informit Network