S
stuart_faircloth
I have the following class:
public class Class1 : IEnumerable<int>
{
List<int> values = new List<int>();
public IEnumerator<int> GetEnumerator()
{
lock(this) {
foreach (int i in values) {
yield return i;
}
}
}
If i iterate over an instance of class1 using the foreach operator how
will the instance of Class1 be locked (see lock inside
Class1.GetEnumerator). Is the lock just acquired once when the
GetEnumerator is first invoked? Is it acquired and released each time
the CLR calls the GetNext() on the generated Enumerator? or is the
lock acquired for the whole of the foreach and then released at the
end?
Thanks
public class Class1 : IEnumerable<int>
{
List<int> values = new List<int>();
public IEnumerator<int> GetEnumerator()
{
lock(this) {
foreach (int i in values) {
yield return i;
}
}
}
If i iterate over an instance of class1 using the foreach operator how
will the instance of Class1 be locked (see lock inside
Class1.GetEnumerator). Is the lock just acquired once when the
GetEnumerator is first invoked? Is it acquired and released each time
the CLR calls the GetNext() on the generated Enumerator? or is the
lock acquired for the whole of the foreach and then released at the
end?
Thanks