Wrox's C # Advanced Programming Third Edition Part 1 Chapter 1 (9 ~ 12)

xiaoxiao2021-03-06  93

1.3.2 value type and reference type

Like any other programming language, IL provides many pre-defined raw data types. However, the distinctive difference type and application type are also an important feature of IL. The value type is those types that directly store data values, and the reference type just stores the address of the memory unit where the data value is located. With this address, we can find the appropriate data.

In C terms, similar to the reference type is a pointer, and in Visual Basic, the object is most similar to the reference type here, and the object in Visual Basic is always accessed by reference. The IL also made detailed specifications because of the storage citation type, which is always stored in memory called hosted stacks, and the variable of the opposite value is usually stored in the stack (although a value type is defined in a reference type. However, they are still stored in the pile of inline). We will discuss stacks and piles in Chapter 3, and how they work.

1.3.3 strong data type

An important feature of IL is that it is based on strong data type exception check. That is to say that all variables in IL must be clear to indicate specific data types (eg, the Variant data types allowed in Visual Basic and scripting languages ​​are not existing). In particular, IL does not allow operations that cause ambiguity data.

For example, because Visual Basic 6 can automate type conversion, Visual Basic 6 developers can pass the variables until too much consider their type. C developers are used to convert pointers in different types. Performing such an operation can greatly improve performance, but this also destroyed the type of security. Therefore, this is only allowed to be allowed in certain languages ​​that need to be compiled as a managed code. It is indeed this, the pointer (relative to the reference) is only allowed in the labeled C # code block, and the pointer is not used in Visual Basic (although it is allowed in managed C ). The use of the pointer in the code will result in failure in the type security check execution in the CLR.

You should notice that there are still some .NET still allows you to relax in the type check, such as Visual Basic .NET, but it is only the compiler to determine such types to ensure the type security of the background IL.

Although mandatory types of security checks may result in performance loss, in many cases, the benefits of various services provided by .NET far exceed the performance losses caused by type security checks. These services include:

❑ Language interoperability

❑ Garbage collector

❑ Security

❑ Application domain

Let us let us understand why strong data types are particularly important.

1.3.3.1 Importance of strong data types on language interoperability

If a class is a class that is derived, or if you want to include other classes, he needs to know the data type used by other classes. This is why the strong data type is such an important reason. Indeed, in the past is the detailed specification of this data type information, always hinders the mutual inheritance between languages, and achieving interoperability. This type of information is not simple in the standard executable or DLL file.

Assuming a method for a class of a Visual Basic .NET defines a return value is a "integer" -Visual Basic .NET type of standard data type. But C # does not have a data type of this name. Obviously, if the compiler knows how to make a constant map of Visual Basic .NET to C # already defined some type, we can inherit, call methods, call methods, and return values ​​in Visual Basic .NET. .

General type system

The problem of this data type. Net is solved by the Common Type System (General Type System, CTS). The CTS defines some predefined data types that can be used in IL, so all the language that is hoped to run with the .NET Framework, which needs to be generated on these types of code. Considering the example of our just mention, the integer data type of Visual Basic .NET is actually a 32-bit band symbol integer, which is completely consistent with the data type INT32 in IL. So this is the data type of IL code. Because C # also knows this data type, the previous problem has been solved. In the source code, the keyword corresponding to the INT32 is int, so it only needs to simply make a conversion, convert the return type of the Visual Basic .NET method to INT.

CTS is not only the basic data type, but he also defines a rich type hierarchy, which contains a defined point, and other languages ​​can define their own types on this. The hierarchy of the Common Type System reflects IL's single inheritance object-oriented methodology. As shown in Figure 1-1:

Picture 1-1

The table below is used to illustrate the types mentioned in the above.

Types of

Meaninglessness

Types of

Base class, any type is inherited in him

Value type

Base class, any value type is inherited in him

Reference type

Access by reference, and stored in the stack

Built-in value type

Including most standard basic types, used to represent numbers, Boolean, characters

Enumerated type

Collection of enumerated values

User definition value type

In the source code definition and save the data type of the value type. In the C # language, all structures are this

Interface Type

interface

Pointer type

pointer

Self-description type

To provide your garbage collector, you can use the benefits of the garbage collector, such a type is the type of self-description (see the next section)

Array

Type of data containing a batch of objects

Class type

In addition to self-description types other than arrays

Delegate

Designed to save applications for methods

User definition reference type

Define in the source code and saved as a data type of the reference type. In the C # language, all classes are this

Package value type

Type the temporary packaging value in one reference, this value type can also be stored in the heap

We don't listen to all built-in value types here because we will discuss them in detail in the second chapter. In C #, each predefined type will be mapped to the built-in type in the corresponding IL. The same is true in Visual Basic .NET.

Public language norm

转载请注明原文地址:https://www.9cbs.com/read-102006.html

New Post(0)