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.
```
lock(_names) return _names;

```

is short for:

```
try
{
  Monitor.Enter(_names);
  return _names;
}
finally
{
  Monitor.Exit(_names);
}

```

which is the equivalent of:

```
Monitor.Enter(_names);
Monitor.Exit(_names);
return _names;

```

seeing it that way should make it more clear that the lock is not necessary.
You might be interested in using one of the [thread-safe collections](https://stackoverflow.com/questions/2980283/thread-safe-collections-in-net)