c# - Use field of class that implements Singleton pattern in another class -
i have class a implements singleton pattern , contains object obj: 
public sealed class {     static instance=null;     static readonly object padlock = new object();     public object obj;      a()     {         acquireobj();     }      public static instance     {                 {             if (instance==null)             {                 lock (padlock)                 {                     if (instance==null)                     {                         instance = new a();                     }                 }             }             return instance;         }     }     private void acquireobj()    {       obj = new object();    } }   now have class b need keep instance of a.obj object until it's alive.
public class b {     // once class instantiated, class b should have public a.obj     // field available  share.     // best way/practice of putting obj here?  }   thank you.
just make this:
class b {     public object obj     {                 {             return a.instance.obj;         }     } }   if it's first time touches a.instance, initialize it. on subsequent calls reuse same instance of a.
Comments
Post a Comment