Thinking about the problem that the list add element overwrites the previous element, listadd

Source: Internet
Author: User

Thinking about the problem that the list add element overwrites the previous element, listadd

A very interesting problem was found in the code wall recently. It defines a reference object. If the object is defined outside the loop, list in the loop. add (object), but the final result is that all object values are the same, that is, each add will overwrite all the previous data, which is quite interesting, you can easily find the answer on the Internet and create a new object in the loop. Although the problem has been solved, I feel that some of the information is included here. net underlying storage knowledge, about the storage method of reference type and value type, wrote a demo to summarize, level, some cattle found that there are deficiencies in the demo please correct.

See the following two figures:

Figure 1: Define object a on the outside. The debugging interface shows that when I = 4, all objects in the previous list are overwritten.

Figure 2: Define object a in a loop and it will not be overwritten

I tried it again using the value type (int, short, and so on). It won't be overwritten outside the loop, and the result won't be able to get the result. After the experiment, I will summarize it in a single sentence: for List <T>, if T is a reference type, it stores references. If T is a value type, it stores the value itself!

However, the preceding summary contains a special column: string type. String is very special in. net. The. net official put it into the reference type, but it is very similar to the value type. For details, refer to the article about the wind of park friends: http://www.cnblogs.com/yank/archive/2011/10/24/2204.

The following figure shows the experiment result using string:

Obviously, string does not overwrite the previous data and is a special column in the reference type.

I have thought deeply about the reason for coverage (the level is limited. You are welcome to add it). It should be as follows:

1. for the reference type, after the object is added outside the loop, the reference address of this object is determined and executed to the second list. when adding (), the Object saved in list [0] and the newly added list [1] a object are the same object and use the same address, that is to say, when list [1] is added, list [0] is also modified because the two pointers point to the same address. Similarly, all the objects added later will be modified, the result is that all data in the list is the final list [end_num].

2. string is also a reference object, with a unique reference address (assuming address1 is assigned), but when the add list [1] (when the str value is changed ),. net checks the memory and finds that the string is not equal to the previous one (list [1] is not equal to list [0]). net will re-allocate the memory space address2 to the original str, store list [0], and list [1] uses the original space address1. In this way, the previous elements are not overwritten each time list. add is used because they use different address spaces.

This also explains that in my previous socket project, data is received using StringBuilder instead of string. Each time a string is changed, a portion of memory will be consumed, and the string object is frequently processed in socket reception, it will consume a lot of memory.

I feel that this small experiment has dug up a lot of things. I need to study it in depth.

 

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.