Value Type. Further dive into it!

Source: Internet
Author: User

1) first define a structure

Struct person
{
Public int age;
}

2) Then pack the Value Type

Static void main (string [] ARGs)
{
Person P = new person ();
P. Age = 1;

// Bind the Value Type
Object OBJ = P;

P. Age = 2;

Console. writeline (P. Age );
Console. writeline (person) OBJ). Age );
}

Final result output: 2 and 1

Summary:
When the value type is boxed, a copy of the current value type is actually obtained, instead of being referenced (usually references are passed between classes ), to put it simply, the information of this value type is copied to OBJ. After copying, OBJ no longer depends on the original person p. the re-assignment of age does not affect obj.
If it is not a struct person but a class person, the final output result will be 2, because the reference to Variable P is assigned to OBJ.
In the same way, if a variable of the int equivalent type is packed, the result will be the same as above.

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.