以及那代碼List<Person> people
不應該受到第二行的影響。
反正List<Person>
得到一個參考,如果您使用.Add()
所以如果你修改這個人後,人在列表中「修飾」過(這是同一個人),但如果你指定的人給別人你'不影響參考,您將符號分配給新的參考。
如:
List<Person> people = new List<Person>();
Person dan = new Person() { Name="daniel", Age=10 };
people.Add(dan);
/* DanData = { Name="daniel", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
dan.Name = "daniel the first";
string dansNameInList = people[0].Name; /* equals "daniel the first" */
/* DanData = { Name="daniel the first", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
people[0].Name = "daniel again";
string dansName = dan.Name /* equals "daniel again" */
/* DanData = { Name="daniel again", Age=10 };
* `people[0]` maps to "DanData"
* `dan` maps to "DanData" */
dan = new Person() { Name = "hw", Age = 44 };
string dansNameInListAfterChange = people[0].Name /* equals "daniel again" */
string dansNameAfterChange = dan.Name /* equals "hw" */
/* DanData = { Name="daniel again", Age=10 };
* NewData = { Name = "hw", Age = 44 };
* `people[0]` maps to "DanData"
* `dan` maps to "NewData" */
你在哪裏添加一個對象列表?你能修復你的代碼嗎? – Arseny 2010-06-30 08:51:07
哎呀!忘了添加一個對象。謝天謝地,stackoverflow的聰明頭腦理解了我想傳達的內容。 – Yeonho 2010-06-30 14:01:01