Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose the singleton object in .NET

Tags:

.net

ooad

I have two classes ClassA and ClassB both having a reference to a singleton object ClassHelper. My question is how should i dispose the singleton object once im done using both the ClassA and ClassB

Edit:

public ClassA
{
  CHelper obj;

  public ClassA()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncA();
  }
}

On the same lines
public ClassB
{
  CHelper obj;

  public ClassB()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncB();
  }
}


where 

CHelper
{
   private static sm_CHelper;

   public static GetInstance(string strInitialise)
   {
      if(sm_CHelper == null)
      {
         sm_CHelper = new CHelper(strInitialise);
      }
   }

   private CHelper(string strInitialise)
   {
      //do something here 
   }

   public CallFuncA()
   {
     // do something here
   }
   public CallFuncB()
   {
     // do something here
   }
}

Regards Learner

like image 206
constant learner Avatar asked May 20 '26 08:05

constant learner


1 Answers

if you are talking about the pattern singelton then you should not dispose it.... if your not referring to the singelton pattern then you could try to use the deconstructor to run your dispose logic.

like image 128
Peter Avatar answered May 21 '26 21:05

Peter