473,402 Members | 2,061 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,402 software developers and data experts.

Unsure about destructor & IDisposable for C#

I would like to do things when my object goes out of scope, like
disconnect TCP/IP streams, serialize collections to files and so on... Would
I implement my cleanup code in Dispose() ?

Is this correct?
class A : IDisposable
{
A()
{
}

~A()
{
Dispose();
}

public void Dispose()
{
//clean up stuff here
}
}
Nov 13 '05 #1
1 2827
That is partially correct. Check out the docs for how MS implements
IDisposable in their objects. The key thing is that if the Dispose
method is called, that you also call GC.SupressFinalize(). You will
also need a flag to indicate that the Dispose method was called vs the
finalizer.

Jonathan Schafer

On Sat, 12 Jul 2003 12:37:06 -0400, "Trevor Balcom" <tb*****@attbi.co>
wrote:
I would like to do things when my object goes out of scope, like
disconnect TCP/IP streams, serialize collections to files and so on... Would
I implement my cleanup code in Dispose() ?

Is this correct?
class A : IDisposable
{
A()
{
}

~A()
{
Dispose();
}

public void Dispose()
{
//clean up stuff here
}
}


Nov 13 '05 #2

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

Similar topics

11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For...
5
by: Chris Ashley | last post by:
I have inherited some code and I am debug some connection pooling errors. Is there anything wrong with the implementation of this class? I am just curious about the empty destructor and...
13
by: Peter Hemmingsen | last post by:
Hi, I have written a dotnet class that in its constructor consumes a license from a central license pool. In the destructor it free the license again. But since I don't know when the destructor...
7
by: Lloyd Dupont | last post by:
I create 2 ManagedC++ class inherithing from each other. I can't see how do I call super destructor or ensure it's called :-/ template <class T> public ref class CArray : System::IDisposable {...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
3
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
2
by: Scott McFadden | last post by:
I have a C++ CLI Managed DLL project which is consumed by C# clients. What is the recommended way for ensuring proper clean up of managed resources and native resources in a managed C++ class? ...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.