Post

When is ToArray required

Why is ToArray required in certain situations?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class LinqTests
{
    private static int _count1 = 0;
    private static int _count2 = 0;

    [Fact]
    public void DelegateCaller()
    {
        IEnumerable<A> list = new List<A>()
        {
            new A(1, 1),
            new A(1, 2),
            new A(1, 3),
            new A(2, 1),
            new A(2, 2),
            new A(2, 3)
        }.AsEnumerable();

        Func<A, bool> selector1 = (a) =>
        {
            _count1++;
            return a.a > 1;
        };
        Func<A, bool> selector2 = (a) =>
        {
            _count2++;
            return a.a > 1;
        };


        var ienumerable = list.Where(selector1);
        var array = list.Where(selector2).ToArray();

        var threshold = Enumerable.Range(0, 2);

        foreach (var item in threshold)
        {
            var choosed1 = ienumerable.Where(o => o.b > item);
            var choosed2 = array.Where(o => o.b > item);

            foreach (var rec in choosed1) { }

            foreach (var rec in choosed2) { }
        }

        int listCount = list.Count();
        Assert.True(_count1 == listCount * threshold.Count());
        Assert.True(_count2 == listCount);
    }


    record A(int a, int b);
}

This post is licensed under CC BY 4.0 by the author.