Trying to understand when it's considered best-practice to lock a static variable or not. Is the static Instance setter thread-safe? If not, should it be and why (what's the consequence of not making it thread-safe)?
class MyClass
{
private static MyClass _instance;
private static readonly object _padlock = new object();
public static MyClass Instance
{
get
{
if(_instance == null)
{
lock(_padlock)
{
if(_instance == null)
{
_instance = new MyClass();
}
}
}
return _instance;
}
set => _instance = value;
}
}