18.1.4 chain derivation method: Why can't the value type be null and the reference type be null?

Source: Internet
Author: User

Chain derivation method: in a process of proof, or in a complicated process of reasoning, the conclusion of the previous reasoning is used as the premise of the latter reasoning, and is derived step by step, until the expected conclusion is introduced.

In the previous knowledge, we learned that the value type is stored in the stack, while the reference type is stored in the managed heap, the stack works in the following way: first ensure that the memory variables are allocated and then released. As you can imagine, the variables in the stack are released from the back to the front, this ensures that the advanced rules in the stack do not conflict with the lifecycle of variables.

You can think about some rules about structured programming. C # requires that variables be declared first defined and then used, the life cycle of a variable starts from its definition until the control of the program leaves the variable's {}. The following code describes the fact that we are very familiar.

Static void main (string [] ARGs)

{

Int K = 10; // The Life Cycle of K begins.

For (INT I = 0; I <= 10-1; I ++) // the lifecycle of I starts.

{

Int M = K + I; // the lifecycle of M begins.

For (Int J = I; j <= 10-1; j ++) // the lifecycle of J begins.

{

Int n = J * I; // The Life Cycle of N begins.

// The lifecycle of N is over

} // The lifecycle of J is over

// The lifecycle of M is over

} // The lifecycle of I is over

// The lifecycle of K is over

}

The following figure describes the lifecycle and storage of these variables.

Figure

At the same time, we can find a very interesting phenomenon in C # programming, that is, we can not set the value type to null, but can set the reference type to null.

Assume that the above Code is changed to the following format:

Static void main (string [] ARGs)

{

Int K = 10; // The Life Cycle of K begins.

For (INT I = 0; I <= 10-1; I ++) // the lifecycle of I starts.

{

Int M = K + I; // the lifecycle of M begins.

K = NULL; // the lifecycle of K is over ???

For (Int J = I; j <= 10-1; j ++) // the lifecycle of J begins.

{

Int n = J * I; // The Life Cycle of N begins.

// The lifecycle of N is over

} // The lifecycle of J is over

// The lifecycle of M is over

} // The lifecycle of I is over

// The lifecycle of K is over

}

You should consider the seventh line of code.

K = NULL; // the lifecycle of K is over ???

What should stack do at this time? Imagine what will happen to the stack chart? Obviously, when the stack reaches the third step, it does not know how to destroy the variable k.

Figure

So why can the reference type be set to null? Let's first look at the following code:

Static void main (string [] ARGs)

{

System. Collections. arraylist K = new system. Collections. arraylist (); // the lifecycle of the K begins.

For (INT I = 0; I <= 10-1; I ++) // the lifecycle of I starts.

{

K. Add (I );

Int M = K. Count; // the lifecycle of M begins.

For (Int J = I; j <= 10-1; j ++) // the lifecycle of J begins.

{

Int n = J * I; // The Life Cycle of N begins.

// The lifecycle of N is over

} // The lifecycle of J is over

// The lifecycle of M is over

} // The lifecycle of I is over

// The lifecycle of K is over

}

The processing in the corresponding stack is roughly

Figure

We can see that the variable k is still allocated to the stack, but the region where the arraylist instance is actually stored is stored in the heap. The arraylist instance is indirectly pointed to by the variable K in the stack. The advantage is that the two concepts of null and lifecycle can be separated.

Please read the following code carefully

Figure

We can clearly observe that the lifecycle of K has not changed, but when K is null, the variable k no longer points to the valid address in the heap.

Now we understand that because the value type variables directly store data in the stack, data cannot be destroyed in any form before the end of the lifecycle, the reference type variable stores data in heap. Therefore, if the null value is assigned, the corresponding data in heap is destroyed rather than the lifecycle of the variable.

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.