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.
In this scenario your lock is effectively doing nothing. The getter roughly expands into the following

```
public List<string> names {
  get {
    Monitor.Enter(_names); 
    try {
      return _names;
    } finally {
      Monitor.Exit(_names);
    }
  }
}

```

This means access to the resource is synchronized only for the time it takes to read the reference. It will do nothing to protect the integrity of the `List<string>`. 

The `names` reference is only read once in the `foreach` so it won't be locked during the body . It won't lead to any deadlocks but it won't do much for protection either