C # List method stored in question

Encounter a bug, Zhuaer Nao plug for a long time have not been resolved, it is necessary to record it.

Now I use a multidimensional list.

IList<IList<int>> list = new List<IList<int>>();

I call the method functions in the main function, using the method list.add function () method to add to the list in sublist. Add the following code:

IList<int> l = new List<int>();
for(int i=0;i<10;i++)
{
    if (条件)
    {
        l.clear();
        list.Add(sublist);
    }
}

 

Add a total of three elements

But in the final list, but still only a third sublist, the first two sublist exists , but is empty . This is very strange, since the first two elements are present, then the representative list.Add (sublist) statement is executed successfully, but why is it empty?

I suspect the first time before the value of the Add method will be overwritten, so I went in the main function manually added two sublist, print found out, it can be added later successfully written, not only within the loop.

This code is written in vscode inside, because it is not stepping to view local variables, delayed a lot of time. So I put all of the code moved to the VS2017, I began to single-step debugging (VS2017 Dafa is good!)

I step by step debugging, found that a sublist first write is successful, to the second moment sublist to be written, brush! list [0] value becomes empty!

Now the problem found, but I do not understand where the problem lies.

Finally online search to solutions . If you use the same turned out to be a sublist in a loop, then each will add to overwrite before. The solution is not to define sublist outside the loop, but is defined in the inner loop.

The explanation given by the following method:

After 1. For reference types, the outer loop of a new object that references the object's address is determined, to execute the second list.add (), list [0] stored in a list of objects and the newly added [1] a target are the same object, using the same address, that is to say the addition of list [1] is, list [0] is also modified because Talia pointer to the same address, the same added later the modification will be in front of all objects, the result is the last list all the data are final list [end_num].

2.string also reference object has a unique reference address (assuming that the assignment address1), but add list [1] when (str when the value changes) ,. net checks memory, before the string is not found equal to ( list [1] is not equal to list [0]) ,. net will str original reallocate memory space address2, stored list [0], the list [1] using the original space address1. Such that the element will not be overwritten before each list.add, because they use different address spaces.
----------------
Disclaimer: This article is the original article CSDN bloggers "IT mountaineer", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/qq_35409640/article/details/71404203

But then, I found two issues in further debugging:

1. If the removal l.clear (), is a normal procedure, list all the normal value, only added l.clear (), the first few values ​​list will be erased.

2. Add this cycle in the main function (including l.Clear ()), list can be written correctly

Combined with this method, I think the reasons covered are as follows:

Now all types of applications in .net are considered to be referenced object, when the add, .net will be unified reallocate memory. But before you add, l l point to the address, check the information you know, clear the memory does not clear the list, presumably only erase the address, it will have neither given nor value.

As for why this phenomenon does not occur in the main function, I have no better ideas, probably because the list is sure, no reference function in the main, there is no reference in the process list, you may add a method to directly write into the list. Limited level, you are welcome to add.

Guess you like

Origin www.cnblogs.com/masonmei/p/11546170.html