C # Make improvements every day-reference type and Value Type

Source: Internet
Author: User

// Reference type (because 'class' is used) Class SomeRef {public Int32 x;} // value type (because 'struct 'is used) Struct SomeVal {public Int32 x ;} static void ValueTypeDemo () {SomeRef r1 = new SomeRef (); // allocate SomeVal v1 = new SomeVal () on the stack; // allocate r1.x = 5 on the stack; // pull pointer v1.x = 5; // modify the Console on the stack. writeLine (r1.x); // displays "5" Console. writeLine (v1.x); // Similarly, "5" SomeRef r2 = r1; // copy only references (pointer) SomeVal v2 = v1; // allocate and copy members on the stack r1.x = 8; // r1.x and r2.x both change v1.x = 9; // v1.x changes, but v2.x does not change the Console. writeLine (r1.x); // displays "8" Console. writeLine (r2.x); // displays "8" Console. writeLine (v1.x); // displays "9" Console. writeLine (v2.x); // display "5 "}

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.