Question: Alright, so here is the property I have

```
public List<String> names{
    get{
        lock(_names)
            return _names;
       }
   set{
       lock(_names)
           _names = value
      }
}
private List<String> _names;

```

and now say I do a foreach on names like this

```
foreach(String s in names)
{
    Console.WriteLine(s);
}

```

My question is, is `names` locked through the whole `foreach`, or does it only `lock` each time `s` is set, then unlocks inside the `foreach`.

If that's confusing, say I try to do this

```
foreach(String s in names)
{
    lock(names)
        Console.WriteLine(s);
}

```

Will I end up in a deadlock?

Answer: 
The `get_Names` method will only get called once, and the lock will be over (there will be **no** lock) during the iteration of the items. This is probably not what you intend and you should probably go for a much granular lock, such as:

```
lock (someLockObject)
{
    foreach(String s in names)
    {
        Console.WriteLine(s);
    }
}

```

When you lock inside the loop, you will take many locks one after the other, which is almost certainly not what you want, since operating the foreach loop will not be atomic anymore.
It's not locked inside the `foreach`. It's locked once, to `get` a reference, then iterated. (Also, your `lock` statements inside the property are currently unnecessary; those operations are atomic.)

Most likely, you should be locking outside the body of the loop, and where the collection is modified.

---

In response to your edit, no. That's not a parallel `foreach` so it's impossible for the loop to get stuck just by itself, let alone deadlock. Once again, that would just be wasted processing power.