Dispose of the singleton object in .NET.

I have two classes ClassA and ClassB having a reference to a singleton ClassHelper object. My question is how should I delete a singleton object after it has been done using the ClassA and ClassB classes

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
   }
}

Relations Learner

+3
source share
3 answers

if you are talking about the singelton pattern, then you should not dispose of it .... if you are not referring to the singleton pattern, then you can try to use the deconstructor to run your layout logic.

+1
source

. ClassA ClassB.

0

I have never seen such an example. I would probably do something like:

 class Resource {

     static Resource Instance  = new Resource();
     static int count = 2;

     public Resource CheckOut() { 
        if (--count <= 0)
            Instance = null;
        return Instance;
     }
 }

Thus, after checking the ClassA and ClassB resource, the static link ceases to support it. After ClassA and ClassB lose the link to the resource, the finalizer receives the next round of garbage collection.

0
source

Source: https://habr.com/ru/post/1735548/


All Articles