String reference type

Source: Internet
Author: User

First run the Code:

      string b = "123";      private void Form1_Load(object sender, EventArgs e)      {          Test(b);          MessageBox.Show(b);      }            public void Test(string a )      {          a = "456";      }

String is of the reference type and should be passed by reference. The output result should be: 456. The correct answer is: 123.

String inherits from the object rather than system. valuetype (int32 inherits from system. valuetype ). String is a reference type, but this special class shows the value type characteristics.

Why? The original text on Soufun is as follows: a notable feature of string is its immutability. Once we create a string, we allocate a continuous memory space for it on the managed heap, we will not be able to modify this string in any way to make it longer, shorter, or change the format. All operations on this string (for example, call toupper to obtain the string in uppercase format) and return the string. In fact, the other re-created string does not change itself. A String object is called an immutable (read-only), because once this object is created, the value of this object cannot be modified. Sometimes it seems that the string has been modified. Actually, A New String object will be created every time the value is changed, and the variable will point to this new object, the original object is still directed to the original object, so it will not change. This is also the cause of low string efficiency.

The value of the string cannot be changed, but cannot be changed. When the value of B = "123" changes, check the memory and find that this string does not exist. Then, re-allocate the memory space, store "456", and allocate the memory address of, A is local, GC will be released later, and B still points to the "123" address.

Another special feature of string is its memory allocation. In this example, B = "123" saves "123" in the memory, if you create another string object with a value equal to "123", c = "123", the memory space is not re-allocated, instead, it assigns the previously saved "123" Address to the reference of C.

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.