Back to questions
JuniorTheoryCommon
What does the program print?
List<int> nums = [1, 2, 3];
var big = nums.Where(n => n > 1);
List<int> copy = big.ToList();
nums.Add(4);
Console.WriteLine($"{big.Count()} {copy.Count}");3 2
- Expecting
2 2:Wheredoes not filter on its own line; the query runs whenCount()enumerates it, after4was added. - Expecting
ToList()to stay linked to the list: it runs the query at once and stores a separate list that later changes do not reach.
- What does the program print if
nums.Clear();replacesnums.Add(4);? - Why does changing
numsinside aforeachoverbigthrow an exception?