List of Self-Defining Classes

Hi, I have a question about how List.

I am trying to define a list

List<periodrecord> pl=new List<periodrecord>(4); 

Then I want to add items to the list through a loop loop

When I did this:

 for (i = 1; i < 100; i++) { try { periodrecord pr = new periodrecord(); /*some random lines*/ pl.Add(pr); } catch (Exception e) { break; } } 

My question is: the pr address declared in each loop will be stored in a list. But since the pr variable itself is no longer used by the program, will these places be considered empty and somehow rewritten? Thank you

Given the answer, there are still some doubts, my complete codes are as follows:

  List<periodrecord> pl=new List<periodrecord>(4); for (i = 1; i < 100; i++) { try { periodrecord pr = new periodrecord(); record2 = sr.ReadLine(); SNorPartCode = record1.Split('&')[0]; phototype = int.Parse(record1.Split('&')[1]); System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; time = record1.Split('&')[2]; pr.start = DateTime.Parse(time,provider); pr.SNorPartCode = SNorPartCode; pr.phototype = phototype; if (record2 != null) { pr.end = DateTime.Parse(record2.Split('&')[2], provider); } else { pr.end = DateTime.MaxValue; } pl.Add(pr); record1 = record2; } catch (Exception e) { break; } } 

When I took the line: periodrecord pr = new periodrecord ();

from for loop, lines

 pr.start=... pr.end=.... 

changed all the items in the list.

+4
source share
2 answers

The pr address declared in each cycle will be stored in a list.

No, it will not. The value of the pr variable at the point at which Add is called will be stored in the list. This value is a link - a link to the new object that you created.

It is important to understand that the value of a variable in C # is never an object. It is always either a value type value or a reference. After you think about it and start to clearly understand the differences between variables, their values, references and objects - much more (passing parameters, assignment, garbage collection, etc.) become much clearer.

For example, consider:

 Button x = new Button(); Button y = x; 

There is only one object - Button , created on the first line. There are two variables ( x and y ), and after the second line they have the same value (reference to this object) due to the purpose. They are still independent variables though (assigning a new value to x will not change y ). Consider how this applies to your example — you call pl.Add(pr) , which simply passes the value of pr to the Add method. After that, the variable pr completely irrelevant to the list. So you could:

 pl.Add(pr); pr = null; 

and the second line will not affect the list at all.

+16
source

The excellent answer is above (of course, John Skeet - I can not compete with him). But it seems to me that your main question is how to manage memory in C #. Memory is automatically managed in C # , the garbage collector will automatically take care of releasing references to objects sometime after they go out of scope - that is, at each iteration of the for loop in the case of the pr object. However, the garbage collector will still know that the link is still in use by the list, so it will never release it until the list itself goes outside the scope.

Regarding the updated question: when pr is inside the loop, you add the value pr to the list, which is a reference to this object. Each time you click on the loop, it will be a different link when creating a new object. However, when pr is out of the loop, every time you add to the list, you add the value of pr to the list (which is a reference), only this time the value will be the same regardless of iteration. So, now every element in your list contains a link to the same object, and each element will reflect any changes in this link. If you change an element with an index of 5 or 17 or 97 or any other index in the list, all elements, as well as pr itself, will be changed, since the value of all of them is the same object reference.

+3
source

Source: https://habr.com/ru/post/1494895/


All Articles