473,508 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I write a method that is called when an object goes out of scope?

How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )
Nov 16 '05 #1
5 4842
You cannot, because .NET uses like java a nondeterministic garbage
collector, which means the system does *not* know when exactly your objects
are go out of scope. It inspects sometimes all objects if they are still
reachable, and sweep away the rest.
But you can provide a finalizer (a destructor in C#) which will be called
when an object is *collected*. Though there are some things pay attention to
and you should rarely use finalizer as they affect negatively your programs
performance.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Paul Wilkinson" <pw********@mail.com> schrieb im Newsbeitrag
news:54**************************@posting.google.c om...
How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )

Nov 16 '05 #2
Paul Wilkinson <pw********@mail.com> wrote:
How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )


Objects don't go out of scope - variables do.

You can write a finalizer which will be called before the object is
garbage collected, but that will happen at some time after the object
is no longer referenced. It may not even happen at all.

Why do you need to do this? If it's for resource clean-up, you should
implement the IDispoable interface.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
"Paul Wilkinson" <pw********@mail.com> wrote
How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )


Look up the using() command and the IDisposable pattern.

With these you will get a call when the object leaves the using()-scope, and
it is even exception-save.

Sam
Nov 16 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Paul Wilkinson <pw********@mail.com> wrote:
How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )


Objects don't go out of scope - variables do.

You can write a finalizer which will be called before the object is
garbage collected, but that will happen at some time after the object
is no longer referenced. It may not even happen at all.

Why do you need to do this? If it's for resource clean-up, you should
implement the IDispoable interface.


Just a clarification: implementing IDisposable will not automatically cause
any sort of call when a variable goes out of scope or an object is
collected(unless there is a finalizer of course). It is just a standard
pattern which you should use and call manually, usually by using the using
statement.

Nov 16 '05 #5
"Paul Wilkinson" <pw********@mail.com> wrote
How can I write a method that is called when an object goes out of
scope? I've looked all over, so far I've only found discussions of how
to write methods that don't = )


btw, you find the doc to 'using' at
http://msdn.microsoft.com/library/de...pspec_8_13.asp

Sam
Nov 16 '05 #6

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

Similar topics

2
3040
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
7
2250
by: cgamache | last post by:
Which is a more correct statement? A destructor runs automatically when an object is deallocated. or A destructor runs automatically immediately before an object is deallocated.
13
22884
by: Matthew Wieder | last post by:
In my C# application, I have class which has method that opens an XML document, modifies it and saves it out. I run that method for several different XML documents. What I've found is that the...
2
9150
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
4
2023
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
26
2112
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
6
6847
by: Jeremy | last post by:
I want each instance of an object to be able to listen for input events. When the event occurs, a method of the object should be called, such that "this" is in scope and refers to the object...
9
1427
by: Steve Richter | last post by:
I understand the IDisposable interface and how the using instruction will call the Dispose method of an object. What if a reference to an object is passed to a 2nd thread? And the object...
9
1565
by: VK | last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to beta stage.<OT> Besides that I am planning to write an introductory to inheritance schema currently used in Javascript...
0
7227
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
7127
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...
1
7054
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
7501
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...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.