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(); 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.
source share