473,799 Members | 2,683 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 4853
You cannot, because .NET uses like java a nondeterministi c 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********@mai l.com> schrieb im Newsbeitrag
news:54******** *************** ***@posting.goo gle.com...
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********@mai l.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
"Paul Wilkinson" <pw********@mai l.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Paul Wilkinson <pw********@mai l.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(unles s 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********@mai l.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
3058
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 to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
7
2268
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
23037
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 Load emthod on the MXLDocument isntance loads the document into memory (as it should) but I have no way of releaseing that memory throughout the application, and I run out of memory. The documents I'm loading are abou 10 MB in size, and I run out...
2
9201
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
4
2038
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 the heap and release the memory when it's deleted from the hash table so that GC recollection does not need to run that frequently. I read up examples about Dispose method in MSDN. What I find very strange is that it's all talking about how to...
26
2152
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); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
6
6862
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 instance. Is this possible? Example: function MyConstructor(element) {
9
1444
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 contains a handle to an open file. The Dispose( ) method closes the handle. How would the Dispose( ) method called from the end of the using block in the 1st thread know that references to the object still exists in other threads?
9
1581
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 programming. It will not be a manual of a "proper" way to use OOP inheritance but a compilation so a description of _all_ OOP paradigms that had been used or are being used in Javascript commercial solutions: starting from the prototype inheritance of...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
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.