Debate on. NET types

Source: Internet
Author: User
Tags image processing library

The type is not a class. MajorityProgramMembers know what a class is, but they do not know what a type is.

Standard answer-how many people can get the clear value type and reference type? The answer in is completely correct. And how many people can get the clear value type and reference type? This articleArticleThis interview question in is completely okay. It seems simple, but it requires a deep understanding. Because most programmers know what a class is and do not know what a type is.

In many cases, simple concepts are often the most difficult to understand. For example, what is the price? What is an enterprise? One of the two major contributions made by Cocos, the 1991 Nobel economics prize winner, is to answer "what is an enterprise" ("the essence of an enterprise").

====

Type is an ancient word. The true meaning of this word is not understood by the vast majority of programmers. Research Type people generally design programming languages, write compilers, or play with functional languages. A well-known book, types and programming ages (tapl) of Benjamin C. Pierce. The Chinese translation is "type and programming language". This book has read only a few pages several times, which is too difficult.

According to Benjamin C. PIERCE:

A type system is a syntactic Method for enforcing levels of specified action in programs.

Each language has its own type system. The following is a definition of the type system in Wikipedia:

In computer science, type systems are used to define howProgramming LanguageThe values in the table are categorized as many different types, how these types are operated, and how these types interact. Type can confirm that a value or a group of values has a specific meaning and purpose (although some types, such as abstract types and function types, may not be expressed as values during program running ). The type systems are very different in different languages. Perhaps the most important difference lies in the syntax during compilation and the operation implementation mode during runtime.

If a language enforces a type rule (that is, it generally only allows automatic type conversion on the premise of no loss of information), this processing is called a strong type, and vice versa is called a weak type.

I can still understand the above. The following paragraph is dumb:

There are several types:

Original Type
This is the simplest type, such as integers and floating-point numbers.
Integer type
All are numbers, such as integers and natural numbers.
Floating Point Type
Number Type with floating point number
Composite Type
Types that are composed of basic types, such as arrays or record units. Abstract data types have the composite type and interface attributes, depending on which one you mention.
Child type
Derived type
Object Type
Example: variable type
Incomplete type (brother dumb)
Recursive type
Function Type
Example: Double-argument function
Full name quantification type (Ge Yun)
Such as parameterized type and Type Variable
Quantification type exists (brother looks at the ceiling)
Such as module
Refining type (brother looks out the window)
Recognize types of subsets of other types
Dependency type (brother to WC)
Depends on the type of the value in the running period
Ownership type (brother goes to Xiaoyue)
Describe or constrain the type of the object-oriented system structure

We are not engaged in programming languages and do not need to understand the above section. Intuitively:

(1) The type is not a class, and the type and class cannot be reached. Don't think that the type contains a class word, so they feel that they are relatives and say that the interface is not a joke of the type because they cannot be directly new;

(2) type is a logical concept, which represents an abstract and constraint relationship. Describe the program by using abstract relationships, and ensure program robustness through constraints.

====

The. NET type system is called the Common Type System (CTS ). A common type system is a simple type system, such as the complex types in the preceding type classification. net, it is difficult to find the right thing (maybe f # Can, brother looked down at the floor, but I don't know what the type refers to), playing type people all go to play Haskell. In their opinion, the. NET type system too simple, too naive.

Is the type Architecture of the public-type system described by msdn:

Translate:

Value Type and reference type are the most basic types of. net. The following is an explanation of the value type and reference type on msdn:

Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. value types can be built-in (implemented by the runtime), user-defined, or enumerations. for a list of built-in value types, see. net Framework class library.

Reference types store a reference to the value's memory address, and are allocated on the heap. reference types can be self-describing types, pointer types, or interface types. the type of a reference type can be determined from values of self-describing types. self-describing types are further split into arrays and class types. the class types are user-defined classes, boxed value types, and delegates.

From the above, we can see that the value type and reference type represent two types of constraints:

(1) The value type contains its own data. The value type is allocated on the stack or inline with other struct (structure, non-struct !) ;

(2) The reference type stores a reference to the data on the stack (this sentence is not completely accurate, and the pointer can also point to the data on the stack ).

In this way, the partition value type and reference type are very direct.

Type T = tvalue. If t contains its data, type is the value type. If t only contains references, and the specific value needs to be searched based on memory, it is the reference type.

Data types do not have to have their own data. For example, the interface type directs to the data of the class type or the data of the value type after the class type is boxed. Pointer type, which points to data of the value type.

====

The following is a big debate.

(1) pointer type.

Int * P = xxx. According to the above judgment method, int * is the reference type.

(2) Interface Type

Iinterface I = (iinterface) entity. Here I is just a reference. If entity is a class, it points to the address of the class on the stack. If entity is struct, it points to the address on the stack after the struct is packed.

(3) Debate between class and. Class

In. net, everything is. class, which is the implementation level, while the keyword class is the type level, which represents the "class type ".

====

Some people say this is a language game, but it is not. Type represents constraints. Only by knowing these constraints can you be comfortable with. Net rules. Let's talk about it in depth:

(1) Value Type in managed heap

Here we need to refute a widely spread paradox: the. NET value type is on the stack, and the reference type is on the managed stack.

For example:

Int [] DATA = new int [100000000];

In the above example, the array is of the reference type, and the 0.1 billion int in the array is of the value type. Where do you say the 0.1 billion value types are? It must be on the hosting stack, and the stack cannot be congested. Therefore, my opinion is:

"The Value Type of the referenced data member is inline within the reference data type on the hosting stack ."

Let's take a look at the following two paragraphs.Code:

//
Int [] data0 = new int [100000000];
For (INT I = 0; I <data0.length; I ++)
{
Data0 [I] = I;
}

// B
Int [] data1 = new int [100000000];
Int length = data1.length;
For (INT I = 0; I <length; I ++)
{
Data0 [I] = I;
}

The functions of these two codes are the same. The Int length = data1.length in the B-segment code is redundant, not actually. In the loop body of A, I in the stack is compared with the field inline with data0.length In the heap, while in the loop body of B, is to compare the I in the stack with the length in the same stack. In some cases, performance may vary by an order of magnitude. For details, see my previous blog "performance: Experiment observation on locality and performance".

(2) Value-type memory management

The. Net Value Type is worth exploring. It is essential to write high-performance. net programs and is the only way to use pure. Net code to bypass GC and precisely control the memory.

The. NET memory consists of three parts: Stack: automatically managed by the compiler; managed heap: managed by GC; unmanaged heap: manually managed, manually allocated, and released. Only data of the value type can be distributed on the stack, on the unmanaged stack, or on the unmanaged stack, and on the referenced data (if any ), it must be hosted on the stack.

The value type and pointer type completely bypass GC. This opens two doors for us:

(A) Real-time programming. GC is the biggest obstacle to real-time programming. A large number of value types can bypass GC for real-time programming. You know, real-time programming is very difficult for languages like Java. You even need to design a dedicated Real-Time Java virtual machine, which is completely unnecessary for. net. This is very important for Control Systems with real-time requirements.

(B) High-Performance programming. You can use value types and pointers to perform very fine-grained memory operations and write efficient code:

· You can directly use pointers to operate on value types;

· You Can inline the value type in the class, and the delegate class type uses GC to manage the life cycle of the value type;

· You can allocate the value type to the unmanaged heap and manage its lifecycle by yourself;

· You can allocate the value type to the stack or use stackalloc to allocate the value type array in the stack.

(3) If you do not know about the. NET type system, it may cause problems in program development.

For example, I didn't know how to pack a value type into an interface type. In the image processing library, I was prepared to define several interfaces for each pixel for abstraction. It is normal that an image has millions of pixels and tens of millions of pixels. If you use an interface to operate these pixels, a large number of packing operations will occur, resulting in a sharp decline in program performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.