List add element overlay before element problem thinking

Source: Internet
Author: User

The recent code wall found a very interesting problem, the definition of a Reference object, if you define the object outside the loop, in the loop List.add (object), the final result is all the object values are the same, that is, every add once, will be all the previous data is overwritten, quite interesting, Easy to find the answer on the Internet, the object in the loop new on the line, although the problem solved, But feel the bread contains some. NET underlying storage knowledge, there is a reference type and value types of storage problems, wrote a demo summary, the level, there are Daniel found that there are shortcomings in the demo also please correct me.

As shown in Figure two below:

Figure 1: When you define object a outside, you can see in the debug interface that all the objects in the list were overwritten before i=4.

Figure 2: Define object A In the loop, not overwritten

Again with the value type (int,short, such as) tried, in the loop outside will not be covered, the results will not, after the experiment, with a summary of a Daniel said: for List<t>, if T is a reference type, then save is a reference, if it is a value type, The value itself is saved!

But there is a special column in the summary above: string type. String in. NET is very special,. NET official put it into the reference type, but it is very similar to the value type, specific explanation of the park friends stay Wind This article: http://www.cnblogs.com/yank/archive/2011/10/24/ 2204145.html.

The results of the string experiment are as follows:

It is obvious that the string does not overwrite the previous data, it is a special column in the reference type.

In-depth consideration of the reasons for the coverage (limited level, welcome to add), should be as follows:

1. For reference types, when a object is new outside the loop, the reference address of the object is determined, and when executed to the second List.add (), the A object saved in List[0] and the new list[1] A object are the same object, using the same address, that is, adding a list [1] Yes, list[0] has also been modified, as both pointers point to the same address, and as a result, all previous objects are modified, and the results are the last of all the data in the list List[end_num].

2.string is also a reference object with a unique reference address (assuming Address1 is assigned), but when add list[1] (the str value changes),. NET checks the memory and finds that it does not equal the previous string (list[1] is not equal to list[0]). NET will reallocate memory space Address2 to the original STR, store list[0], and list[1] use the original space Address1. This allows the previous elements to be overwritten each time they are list.add, because they use different address spaces.

This also explains that my previous socket project was receiving data using StringBuilder instead of string, each time a string is changed, it consumes a portion of memory, and the socket receives frequent processing of the string object, which consumes a lot of memory.

Feel this small experiment dug out a lot of things, to delve into it.

List add element overlay before element problem thinking

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.