मैं कुछ कोड देख रहा हूं जो एक सहकर्मी ने लिखा था और मुझे क्या होने की उम्मीद नहीं है। यहाँ कोड है:यह समझने की कोशिश कर रहा है कि इस मामले में स्थिर कैसे काम कर रहा है
public class SingletonClass
{
private static readonly SingletonClass _instance = new SingletonClass();
public static SingletonClass Instance
{
get { return _instance; }
}
private SingletonClass()
{
//non static properties are set here
this.connectionString = "bla"
this.created = System.DateTime.Now;
}
}
अन्य वर्ग में, मैं ऐसा करने में सक्षम होने की उम्मीद होता:
private SingletonClass sc = SingletonClass.Instance.Instance.Instance.Instance.Instance.Instance;
और यह उस वर्ग के एक ही उदाहरण के संदर्भ हैं। क्या होता है कि मेरे पास केवल .Instance
हो सकता है। कुछ ऐसा जो मैंने उम्मीद नहीं की थी। यदि Instance
संपत्ति SingletonClass
कक्षा लौटाती है, तो मैं उस लौटे वर्ग पर Instance
संपत्ति क्यों नहीं कह सकता हूं और इसी तरह?
ओह ठीक है, उस अर्थ अब बनाता है यह
SingletonClass _instance = new SingletonClass();
करने से आप की बचत है का एक उदाहरण लौटा रहा है। – Justin