PC Review


Reply
Thread Tools Rate Thread

destructing static member variables

 
 
=?Utf-8?B?VmVua2F0?=
Guest
Posts: n/a
 
      4th May 2005
This is a C# question. If this is not the right place to for this question,
Please point me to a different news group.

Are there any static destructors in C#? I found there is no such concept. In
that case, How can one destruct the static member variables intialized in
static constructor? Is there any work around?


 
Reply With Quote
 
 
 
 
David Browne
Guest
Posts: n/a
 
      4th May 2005

"Venkat" <(E-Mail Removed)> wrote in message
news:6B152924-CBB0-4694-8FB2-(E-Mail Removed)...
> This is a C# question. If this is not the right place to for this
> question,
> Please point me to a different news group.
>
> Are there any static destructors in C#? I found there is no such concept.
> In
> that case, How can one destruct the static member variables intialized in
> static constructor? Is there any work around?
>


The object which you assign to a static variable will be garbage collected
when the AppDomain is unloaded. If any cleanup is required, you can give
that type a finalizer. As a trick you can assign a finalizable type to a
static readonly variable to act as a "static finanalizer".

class Foo
{
class StaticFinalizer
{
~StaticFinalizer()
{
//Foo's cleanup code here
}
}
static readonly StaticFinalizer staticFinalizer = new StaticFinalizer();

. . .
}

The StaticFinalizer instance will stay alive until the AppDomain is
unloaded. Subsequently it will be collected and the finalizer should run.
NB there are exceptional situations in process shutdown where pending
finalizers will not be run.

David


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Static member variables =?Utf-8?B?Q29saW4gRGVzbW9uZA==?= Microsoft VC .NET 3 29th Nov 2005 08:01 PM
destructing static member variables =?Utf-8?B?VmVua2F0?= Microsoft C# .NET 1 4th May 2005 02:49 PM
Use of static member variables =?Utf-8?B?VG9tIFBlYXJzb24=?= Microsoft ASP .NET 5 19th Jul 2004 01:34 PM
Static member variables and static libraries Paul Microsoft VC .NET 1 4th Jun 2004 06:53 PM
Bug: specialization of static member variables not supported ion VC7.1 Arne Adams Microsoft VC .NET 2 3rd May 2004 06:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:34 AM.