Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
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).
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.
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.
Handling Memory and Garbage | Next Section

Account Sign In
View your cart