Note that there are some explanatory texts on larger screens.

plurals
  1. POList of self-defined class
    text
    copied!<p>Hi I have a question about how to List. </p> <p>I am trying to define a list by</p> <pre><code>List&lt;periodrecord&gt; pl=new List&lt;periodrecord&gt;(4); </code></pre> <p>Then I wish to add items to the list via a for loop</p> <p>When I did this:</p> <pre><code>for (i = 1; i &lt; 100; i++) { try { periodrecord pr = new periodrecord(); /*some random lines*/ pl.Add(pr); } catch (Exception e) { break; } } </code></pre> <p>My question is: The address of pr declared in each loop will be stored by the list. But since the pr variable itself ceases to be used by the program, will these locations be regarded as empty and somehow be overwritten? Thanks. </p> <p>Considering the answer, there are still some doubts, my full codes are as follows:</p> <pre><code> List&lt;periodrecord&gt; pl=new List&lt;periodrecord&gt;(4); for (i = 1; i &lt; 100; i++) { try { periodrecord pr = new periodrecord(); record2 = sr.ReadLine(); SNorPartCode = record1.Split('&amp;')[0]; phototype = int.Parse(record1.Split('&amp;')[1]); System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; time = record1.Split('&amp;')[2]; pr.start = DateTime.Parse(time,provider); pr.SNorPartCode = SNorPartCode; pr.phototype = phototype; if (record2 != null) { pr.end = DateTime.Parse(record2.Split('&amp;')[2], provider); } else { pr.end = DateTime.MaxValue; } pl.Add(pr); record1 = record2; } catch (Exception e) { break; } } </code></pre> <p>When I took the line : periodrecord pr = new periodrecord();</p> <p>out from the for loop, the lines</p> <pre><code>pr.start=... pr.end=.... </code></pre> <p>altered all the items in the list. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload