[Solved] Does For loop check linq expression at each iteration [closed]


1: No because it is only the starting value. This part will be visited only once at the start of the loop

2: Yes because the linq expression has to be evaluated to get the result value. If you want to avoid it execute the linq expression once before the loop and save it into a separate value:

int end = list.OrderBy(x => x.Key).First(); 
for (var i = list.Count() - 1; i >= end; i--) 
    if (list.ContainsKey(i))
        list.RemoveAt(i)

EDIT:

If your list might change dynamically, then of course the way you use it now would be a preferable solution, since you could accommodate to the changes.

as for question 3: this Answer says that the Add method is not thread safe

3

solved Does For loop check linq expression at each iteration [closed]