Read the Classics-"CLR via C #" (Jeffrey Richter) Notes _ reference type and value type (ii)

Source: Internet
Author: User

"Differences between reference types and value types"

//reference type (due to the use of ' class ')classSomeref { PublicInt32 x;}//value type (due to the use of ' struct ')structSomeval { PublicInt32 x;}Static voidValuetypedemo () {someref R1=NewSomeref ();//allocate on the heapSomeval V1 =NewSomeval ();//Allocate on Stackr1.x =5;//To raise another pointerV1x =5;//Modify on StackConsole.WriteLine (r1.x);//Show "5"Console.WriteLine (v1.x);//Show "5"someref R2= R1;//Copy only references (pointers)Someval v2 = v1;//assigning and copying members in the stackr1.x =8;//r1.x and r2.x will change .v1.x =9;//v1.x will change, but v2.x is unchangedConsole.WriteLine (r1.x);//Show "8"Console.WriteLine (r2.x);//Show "8"Console.WriteLine (v1.x);//Show "9"Console.WriteLine (v2.x);//Show "5"}

Plot

Read the Classics-"CLR via C #" (Jeffrey Richter) Notes _ reference type and value type (ii)

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.