Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of self-defined class

Tags:

c#

Hi I have a question about how to List.

I am trying to define a list by

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

Then I wish to add items to the list via a for 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 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.

Considering the answer, there are still some doubts, my full 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();

out from the for loop, the lines

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

altered all the items in the list.

like image 602
Junting Zhu Avatar asked Dec 01 '22 20:12

Junting Zhu


1 Answers

The address of pr declared in each loop will be stored by the list.

No, it won't. The value of the pr variable at the point at which Add is called will be stored in the list. That value is a reference - a reference to the new object you've created.

It's important to understand that the value of a variable in C# is never an object. It's always either a value type value, or a reference. Once you get your head round that - and start being very clear about the differences between variables, their values, references, and objects - a lot of things (parameter passing, assignment, garbage collection etc) become much clearer.

For example, consider:

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

There's only one object here - the Button created on the first line. There are two variables (x and y) and after the second line they both have the same value (a reference to that object) because of the assignment. They're still independent variables though (assigning a new value to x won't change y). Think about how that is applied to your example - you're calling pl.Add(pr) which just passes the value of pr to the Add method. After that, the pr variable is completely irrelevant as far as the list is concerned. So you could have:

pl.Add(pr);
pr = null;

and the second line wouldn't affect the list at all.

like image 175
Jon Skeet Avatar answered Dec 03 '22 08:12

Jon Skeet