Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Teach Yourself Visual Studio® .NET 2003 in 21 Days [Electronic resources] - نسخه متنی

Jason Beres

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







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.

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.


Tip

You don''t normally get into much trouble when dealing with reference types and value types like the example describes. When you''re creating instances of classes, always derive from a new instance of the object, not a previously set instance.

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.

/ 270