List of Records

前端 未结 3 1267
情书的邮戳
情书的邮戳 2021-01-29 07:06

The following code seems to add a new record to the list but overwrites all the record with the last record created. I can get it to work fine with ...

lpr.Add(n         


        
3条回答
  •  醉梦人生
    2021-01-29 07:15

    Possible duplicate of what is different between Passing by value and Passing by reference using C#.

    But as pr is a class this means that when you are adding it to the list you are actually only storing the reference to that variable. When you change that variable the next 3 times what you are actually changing is the values located at the memory of the original reference. So your list actually contains 4 references to the same object with the same values.

    If you are attempting to reuse the same object (as this may be a model for your view) you should make a new object with it's values and add this new object to your list.

提交回复
热议问题