Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to destroy a Static Class in C#

Tags:

c#

I am using .net 1.1. I have a session class in which I have stored many static variables that hold some data to be used by many classes.

I want to find a simple way of destroying this class instead of resetting every variable one by one. For example if there is a static class MyStatic, I would have liked to destroy/remove this class from the memory by writing MyStatic = null, which is not currently possible,

Additional question.

The idea of singleton is good, but I have the following questions:

If singleton is implemented, the 'single' object will still remain in the memory. In singleton, we are only checking if an instance is already existing. how can i make sure that this instance variable also gets destroyed.

I have a main class which initializes the variable in the static class. Even if I plan to implement a Rest() method, I need to call it from a method, for eg, the destructor in the main class. But this destructor gets called only when GC collects this main class object in the memory, which means the Reset() gets called very late

thanks pradeep

like image 477
pradeeptp Avatar asked Feb 10 '09 06:02

pradeeptp


People also ask

Can we dispose static class?

Answers. Static finalizers and destructors are not possible, because types are only unloaded when the AppDomain shuts down.

Can static class have destructor?

No, there isn't. A static destructor supposedly would run at the end of execution of a process. When a process dies, all memory/handles associated with it will get released by the operating system.

Can static class be dispose C#?

You can have the Dispose method in a singleton class but not in a static class.


3 Answers

Instead of a static class, have a static instance of a class:

class Foo
{
  public int Something;
  public static Foo Instance = new Foo();
  public void Reset()
  {
    Instance = new Foo();
  }
}

void test
{
  int i = Foo.Instance.Something;
}

You can also delegate to an instance of the class:

class Foo
{
  public int Something
  {
    get { return instance.something; }
  }
  private int something;
  private static Foo instance = new Foo();
  public void Reset()
  {
    instance = new Foo();
  }
}

void test
{
  int i = Foo.Something;
}
like image 37
ChrisW Avatar answered Oct 12 '22 18:10

ChrisW


Don't use a static class to store your variables. Use an instance (and make it a singleton if you only want one instance at any given time.) You can then implement IDisposible, and just call Dispose() when you want to destroy it.

For more information check out this site: http://csharpindepth.com/Articles/General/Singleton.aspx

EDIT

The object is still subject to garbage collection, so unless you are using lots of unmanaged resources, you should be fine. You can implement IDisposible to clean up any resources that need to be cleaned up as well.

like image 176
Kyle Trauberman Avatar answered Oct 12 '22 18:10

Kyle Trauberman


There's no way to destroy a static unless it resides in a separate AppDomain in which case you can get rid of it by unloading the AppDomain. However it is usually better to avoid statics.

EDIT: Additional question

When the singleton is no longer referenced it will be collected just as everything else. In other words, if you want it collected you must make sure that there are no references to it. It goes without saying that if you store a static reference to your singleton, you will have the same problem as before.

like image 7
Brian Rasmussen Avatar answered Oct 12 '22 20:10

Brian Rasmussen