DOTNET Learning Note One - Packing Unpacking

zhaozj2021-02-11  193

It has been four years old, and he is also the old man of this line. Looking back, what you have done is not less, drive from the application to the kernel, from normal procedures to web applications, from Windows to Linux. The popular development tool language in the market has also been used, VC, VB, Delphi, BCB, JBuilder, ASP, JSP. But you ask, those who are their own expertise? It seems that there is nothing, it is also a lot of projects when you find a job, and it is fine, it seems that there is no. .NET has also been exposed to, but there is no in-depth, and the previous period of time is due to a chair, and the .NET has created the feelings, so powerful, C # is also a very comprehensive OO language. Plus yourself for many years of feelings for Win platforms, decided to put into the vigorous .NET career. The book has seen the need to digestion and practice. I feel that I have written the things that understand what I understand is a good digestive method. I will blem to write myself into a set of learning notes. This is open, throwing brothers, hopes that all .NET borders are a lot of guidelines, hoping and friends who are like learning to apply .NET technology together, improve.

The .NET is a set of developing platform languages, and it has also proposed a lot of new concepts. Boxing and unboxing should be one.

All types of .NET are inherited by the base class System.Object, including the most common basic types: int, byte, short, bool, etc., that is, all things are objects. However, this causes the efficiency of the bottom, such as simple two numbers, BOOL reversal will result in allocation of memory from the heap. How to solve this problem? .NET divides the type into two categories: value and reference.

Valid type allocates memory in the stack, which is initialized while the declaration is declared to ensure that the data is not NULL. E.g:

BYTE B = 33;

The sucking declaration assigns 8-bit memory in the stack and initializes the variable to 8. The value of .NET includes enumeration (Structure), structure, and basic types (int, float, short, etc.). Value type does not require Garbage Collection to recycle the user's memory. After the scope is exceeded, the system will be automatically released.

The reference type is completely similar to classes in C or Java, allocating memory in the heap, initializing to NULL. The reference model is a need for Garbage Collection to recycle memory.

Since the value is also inherited from System.Object, then such a sentence should be legal:

INT n = 3;

System.Object obj = n;

As mentioned above, the memory of this variable should be allocated in the stack, and OBJ should allocate memory in the stack. What do you do this at this time? The system allocates an object OBJ in the heap and copys the value of n to it. This is called boxing. At this time, N and OBJ are two objects that are not associated, continue to run as follows:

Obj = 9;

Console.writeline ("{0} {1}", n, obj);

The result is:

39

Simplely said that the box is implicitly converted to a reference object.

And the packing correspondence is to remove the box, the unboxing is converted to arbitrary value types. Unlike the packing, the split is the displayed operation. As of the following code: INT i = 0;

System.Object obj = i;

INT j = (int) OBJ;

This can be seen that .NET type system is a unified type system because regardless of whether it is a value or a reference type being considered objects, this allows us to use consistent way to cross coded processing types. Take a look at the following code can clearly understand the benefits of the box:

ArrayList Ar = New ArrayList ();

DATE DT = New Date ();

Ar.Add (DT);

INT n = 10;

ar.Add (n);

As is best two lines of code, we don't have to display a system.object or make a mandatory type conversion. As long as I use our most commonly used Int, you can be processed as other reference objects. In the last line of code, when n is added to the array, it has been automatically contained.

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

New Post(0)