Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected behavior of IEnumerable<T> in C#

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading