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

Source: Internet
Author: User

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

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 Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

C Language & |! What are

& Is the address fetch operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

Related Article

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.