No the yield return
will not cause any locks to be freed / unlocked. The lock
statement will expand out to a try / finally
block and the iterator will not treat this any differently than an explicit try / finally
in the iterator method.
The details are a bit more complicated but the basic rules for when a finally
block will run inside an iterator method is
- When the iterator is suspended and
Dispose
is called thefinally
blocks in scope at the point of the suspend will run - When the iterator is running and the code would otherwise trigger a
finally
thefinally
block runs. - When the iterator encounters a
yield break
statement thefinally
blocks in scope at the point of theyield break
will run