Imagine a ConcurrentDictionary storing a collection of Foo. Is it necessary to worry about thread safety for the Bar property of Foo (e.g. thread 1 getting value of Bar at same time thread 2 setting the value)? Or does the concurrency of the dictionary itself make the property thread safe?
//Bar with no lock
public class Foo
{
public string Bar { get; set; }
}
//Bar with lock
public class Foo
{
private readonly object barLocker = new object();
private string bar;
public string Bar
{
get
{
lock(barLocker)
{
return bar;
}
}
set
{
lock(barLocker)
{
bar = value;
}
}
}
}