C # -- Value Type and reference type

Source: Internet
Author: User

Please advise me about the incorrect description ~ Just learned C #

 

The value type can be directly copied as A = B.

The result of referencing type A = B is that a only saves the address of Data B. At this time, a and B point to the same data and the name is different.

String is also a reference type, but the string content is changed. NET will allocate a new memory space for it to store the new string content, instead of rewriting it on the old address.

So

 

Code
String A =   " I'm coffee " ;< P>

string B;

B = A;

console. writeline (a);

B = " Hello " ;

console. writeline (a);

the output result is:

I'm coffee

B = "hello ",. net applied for a piece of memory to store the hello string, and then returned an address to B, so changes to B do not affect a

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.