我试图使用不同的对象更新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);