I have a question about the result of LINQ and Lambda query. For example, I have the following codes:
class ClassA<T> {
public string Name { get; set; }
public T ObjectT { get; set; }
}
List<ClassA<T>> list;
// list is populated
// First way to get instance from list, reference type?
ClassA<T> instance1 = list.Select(x=> x).Where(x=>x.Name == "A").
FirstOrDefault();
// Second way to clone or copy instance from the list
ClassA<T> instance2 = list.Select(x=>
new ClassA<T> { Name = x.Name, ObjectT = x.ObjectT}).
Where( x=> x.Name = "A").FirstOrDefault();
It is obviously that instance2 is a clone or copy of an instance found in list. How about instance1? Is this one a new instance or just a reference to an instance in the list? If instance1 is an object reference to list's item, any change to its property may change the same object in the list. Is that right?
If that's the case and I don't want to have any implicit effect on objects in the list, I think I should use the second strategy. However, if I do want any changes in the retrieved instances also have the same changes in the list, I should use strategy 1. Not sure if my understanding is correct. Any comments?