【发布时间】:2015-11-14 19:58:12
【问题描述】:
我正在尝试使用不同的对象更新 HashSet 中的对象。在下面的代码示例中,为什么 person2 对象没有被更新?
HashSet<Person> myHash = new HashSet<Person>();
//populate the HashSet with two Person objects
var person1 = new Person() { Id=1, Name = "John", Age = 21};
var person2 = new Person() { Id=2, Name = "Lisa", Age = 25 };
myHash.Add(person1);
myHash.Add(person2);
var person2Updated = new Person() { Id = 2, Name = "LisaUpdated", Age = 25 };
var existingPerson2 = myHash.SingleOrDefault(p => p.Id == 2);
existingPerson2 = person2Updated;
//why isn't the following returning person with Name "LisaUpdated"?
//It is actually returning person with Name "Lisa"
var test = myHash.SingleOrDefault(p => p.Id == 2);
【问题讨论】: