I understand its not recommended to use this for locks as the object can be modified outside of the class whcih is difficult to control. So, its always recommended to use private fields for locks. ( i have looked at : Why is lock(this) {...} bad? )
What about protected locks? i think i should be good as my classes are internal and no one can derive from them outside of my assembly...
Please note that i cannot use seperate locks derived and base class, as deadlock might occur as its difficult to manage in multiple classes.
internal class Base
{
protected object sync = new object();
public string Foo
{
get
{
lock (sync)
{
//set it
}
}
}
}
internal class Derived : Base
{
public string Bar
{
get
{
lock (sync)
{
//set it
//try to get "Foo" (if i use seperate locks-syncobjects
// in derived and base class there can be a ptoential deadlock)
}
}
}
}