c# - When does foreach resolve the container? -
foreach(value v in mydictionary[key])
in scenario, compiler able determine mydictionary[key] same , not hash [key] every iteration of foreach?
foreach(value v in myenumerable.where(s => s.istrue))
i know enumerables lazy, suspect in case, .where resolves once , returns full collection foreach hunch.
judging this question, foreach doing .getenumerable in scenario 1, return of used therefore resolves once , not on every iteration.
foreach
in c# syntactic sugar.
foreach (v v in x) embedded-statement
gets expanded
{ e e = ((c)(x)).getenumerator(); try { while (e.movenext()) { v v = (v)(t)e.current; embedded-statement } } { … // dispose e } }
as can see, x
used once, enumerator calling getenumerator
on it. dictionary lookup executed once. later on care enumerator, not came from.
Comments
Post a Comment