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

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -