I found some unexpected behavior in .NET 6. Could somebody please explain it to me?
Consider the following code:
public class A
{
public string Name { get; set; }
public double? Value { get; set; }
}
public static IEnumerable<A> GetAList()
{
var arr = new int[] { 1, 2, 3 };
return arr.Select(_ => new A { Name = _.ToString() });
}
var aList = GetAList();
foreach (var a in aList)
{
a.Value = 1.0;
}
var aResult = aList.ToList();
The fields Value in aResult are equals to null How can that be possible?
>Solution :
The lambda in arr.Select(_ => new A { Name = _.ToString() }) method is a projection that is, unless you buffer it yourself, evaluated every time you iterate it. Therefore, the data you get when you foreach over the data to assign values is completely separate to (different new A {...} instances) the data you get when you later .ToList() it. Perhaps use
var aList = GetAList().ToList();
so that it actually is a list, i.e. buffered. Now, when you iterate it, it’ll be the same data instances each time.
You can see this in action by having the lambda tell us what it is doing:
return arr.Select(x =>
{
Console.WriteLine(x);
return new A { Name = x.ToString() };
});
You’ll see the numbers are written twice, not once, for the two separate and independent iterations.