473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Is there anything like *static destructor*?

In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is supposed to be
disposed in *static destructor*. However, there is no so-called *static
destructor*. Is there any substitue of *static destructor* or *static
dispose* in C#?

Sep 15 '06 #1
4 12426
Morgan,

No, there isn't. The closest thing you can do is set an event handler
to the DomainUnloaded event on the AppDomain and perform your cleanup there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is supposed to be
disposed in *static destructor*. However, there is no so-called *static
destructor*. Is there any substitue of *static destructor* or *static
dispose* in C#?

Sep 15 '06 #2

Nicholas Paldino [.NET/C# MVP] wrote:
Morgan,

No, there isn't. The closest thing you can do is set an event handler
to the DomainUnloaded event on the AppDomain and perform your cleanup there.

Hope this helps.
Thanks, it really helps.
>

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is supposed to be
disposed in *static destructor*. However, there is no so-called *static
destructor*. Is there any substitue of *static destructor* or *static
dispose* in C#?
Sep 15 '06 #3
Morgan,

There is no static destructor because you cannot finalize or destruct
something that hasn't been instantiated. You may be confused because
we typically call static class initializers constructors in C# which I
think is a bit of a misnomer. If your static references are objects
that override Finalize then their destructors will be called when the
application ends. That's usually sufficient, especially in the case of
a file stream. If you need more control then call a static method on
the class just before your application ends.

Brian

Morgan Cheng wrote:
In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is supposed to be
disposed in *static destructor*. However, there is no so-called *static
destructor*. Is there any substitue of *static destructor* or *static
dispose* in C#?
Sep 15 '06 #4
SP

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is supposed to be
disposed in *static destructor*. However, there is no so-called *static
destructor*. Is there any substitue of *static destructor* or *static
dispose* in C#?
If you use a singleton pattern instead then you can "nullify" the instance.
This is commonly done when doing unit testing and singletons are involved.

PS

Sep 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gorda | last post by:
Hello, I have the following code, with a basic class having a constructor and destructor. in the main section, I create a static pointer to the class. My question is regarding the destructor. If...
7
by: Simon Elliott | last post by:
Consider the following, incorrect, code: #pragma hdrstop #include <condefs.h> #include <iostream> #include <conio.h> class bar {
5
by: Simon Elliott | last post by:
I have a C++ file which needs to work along these lines: class foo { private: int i1_; public: foo():i1_(12){} ~foo() {
3
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
2
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
8
by: jayaramganapathy | last post by:
Hello friends, I have a map like std::map< std::string , std::map<std::string, std::string>* EpPropCache::propertyCache ; (This is a static instance and taken from *.cpp file) As you can see...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.