Consider this code
class Program
{
static void Main(string[] args)
{
var lst = new List<MyObj>()
{
new MyObj() {name = "Old1", weight = 1},
new MyObj() {name = "Old2", weight = 2}
};
lst.Join(GetList(), p => p.weight, q => q, (p, q) =>
{
p.name = "new";
return p;
});
lst.ForEach(p => Console.WriteLine(p.name));
}
public class MyObj
{
public string name { get; set; }
public int weight { get; set; }
}
public static List<int> GetList()
{
return new List<int>() {1,2};
}
}
The output of this code is Old1, Old2. I don't understand why the "name" is not updated inside the resultSelector function. This is my thinking: MyObj instance is a reference type, we are joining it, and collecting it in variable p (which I guess is a reference type, i.e. contains the reference to the original MyObj). So, inside the function to create result, it would update the "name" property (as again a reference would be passed) in the memory location where the original MyObj exists. Why isn't this the case? Where am I going wrong?