使用雙重檢查鎖定是否正確使用非靜態字段?C檢查類成員的雙重檢查鎖定#
class Foo
{
private SomeType member;
private readonly object memeberSync = new object();
public SomeType Memeber
{
get
{
if(member == null)
{
lock(memeberSync)
{
if(member == null)
{
member = new SomeType();
}
}
}
return object;
}
}
}
你真的*試圖實現單例模式嗎?如果是這樣,則有更清晰的模式。請參閱http://csharpindepth.com/Articles/General/Singleton.aspx –