C # Can I change the value after packing the value type?

Source: Internet
Author: User

When a value type is assigned to the reference type, this process can be considered as "Packing ".

object a = 10;

Above, the process on the stack is roughly:
1. Open up space on the stack to variable
2. Open up a space on the stack and think of the space as a "box"
3. Pack 10"

 


Variable A points to an object with a heap value of 10. In other words, the value of variable A is the address of the object on the heap.

 

Now, if we want to make a = 11, the first thing we think of is: let 10 in the "box" increase from 1 to 11.

 

Try to do this:


oject a = 10;a++;


Result error: ++ cannot be applied to the object type. That is to say, ++ cannot be applied to the reference type.

 

It's easy to do. First split the box into a value type, and then increase the value by 1?


object a = 10;((int)a)++;
 

The result still reports an error: the increment or decrement operator must be a variable, attribute, or indexer.
(INT) a) ++, equivalent to 10 + +, equivalent to 10 = 10 + 1, equivalent to 10 = 11, no wonder an error is reported!

 

Okay, ++ won't help us, but we can assign the value after unpacking to another variable:


object a = 10;int temp = (int)a;a = temp + 1;
 

In this way, the value of a is 11.

 

This time, the stack is roughly like this:
1. Open up space on the stack to variable
2. Open up a space on the stack and think of the space as a "box"
3. Pack 10"
4. Open and Close the space on the stack to the variable temp
5. After disassembling the value of A, assign the value to the variable temp. The value of temp is 10.
6. Another space is opened up on the stack.
7. Pack temp + 1 (11)

 


The variable points to an object with a heap value of 11. Objects with a value of 10 on the stack Are waiting for GC collection.

 

Of course, the above method is "saving the nation by curve", which can be like this:


object a = 10;a = 11;
 

This is because you want to get a new value based on the original value.

 

Finally, return to the topic of this article: Can the value be changed after the value type is packed? The answer is no. The value after packing has the characteristics of "immutable. Just like the object A = 10 in this article, if you want to assign a new value to the reference type variable A, you can directly a = 11. 10 once packed, the value cannot be changed.

C # Can I change the value after packing the value type?

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.