What These Problems Cover

  • Enumerable & Array Conversions
  • Yield Behavior
  • Closures in Loops
  • Exception Handling in Queries
  • Foreach Mechanics

Each problem below isolates a specific LINQ behavior in C#. The exercises explore how deferred execution, iterator state, and captured variables interact — often in ways that surprise even experienced developers. Solutions accompany every problem.

Problem Set

3.1

EnumerableToArray

Examines how an enumerable sequence behaves when converted to an array, including the timing of enumeration and potential pitfalls with deferred execution.

3.2

LifeAfterYield

Investigates what happens after a yield return statement — whether code continues executing and how iterator state is preserved across iterations.

3.3

ClosureAndForeach

Explores how closures capture loop variables inside a foreach construct, a classic source of unexpected behavior in LINQ queries.

3.4

ClosureAndFor

Contrasts closure behavior in a standard for loop versus foreach, highlighting differences in variable capture semantics.

3.5

QueryAfterRemove

Tests what occurs when a LINQ query is executed after elements have been removed from the underlying collection.

3.6

ClosureAndVariable

Delves deeper into how closures bind to variables rather than values, and the implications for query composition.

3.7

QueryWithInc

Examines side effects when increment operations appear inside LINQ query expressions, including deferred execution surprises.

3.8

ExceptionYieldYield

Studies exception propagation when multiple yield statements interact — where exceptions are thrown and where they are caught.

3.9

YieldExceptionYield

Explores a variation on exception handling within iterator blocks, focusing on the order of yield and exception points.

3.10

TryYieldFinally

Investigates how try/finally blocks interact with yield return, including when finally clauses execute during enumeration.