473,320 Members | 1,841 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,320 software developers and data experts.

object = null

Hello

I'm pretty new to C#...

Once you don't need an object anymore, do need to kill it or not ?

--> myObject = null;

I know we are using the garbage collector but I don't know how it exactly
works...

Regards
Roel
Nov 15 '05 #1
3 1401

You set the reference to the object to null. Then the garbage collector
will find that the object isn't referenced to anything and clear it.

myObject = null is one way to do it. But for temporary objects that will
fall out of scope you don't need to do even that.

Some classes have a Close() method. And if it has, you should use it
before it falls out of scope. Usually complex classes using resources
that will clean up itself in the Close method.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
thx...
I own you one !

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...

You set the reference to the object to null. Then the garbage collector
will find that the object isn't referenced to anything and clear it.

myObject = null is one way to do it. But for temporary objects that will
fall out of scope you don't need to do even that.

Some classes have a Close() method. And if it has, you should use it
before it falls out of scope. Usually complex classes using resources
that will clean up itself in the Close method.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Nov 15 '05 #3
Hi,
Some classes have a Close() method. And if it has, you should use it
before it falls out of scope. Usually complex classes using resources
that will clean up itself in the Close method.


Some objects implements IDisposable interface.
These objects should be disposed by:
if( obj is IDisposable ) {
obj.Dispose();
}
obj=null;

Marcin

PS: Don't give a name "object" to any variables.
Nov 15 '05 #4

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

Similar topics

11
by: Vani Murarka | last post by:
Hi Everyone, Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for...
3
by: Ian Renfrew | last post by:
Does Javascript supply a function that destroys an object? If so, is there a dependancy on Javascript version? Thanks in advance, Ian Renfrew
10
by: William Stacey | last post by:
Can you use a foreach to set the elements of an object to null? This does not work as o is read-only. Is only way to use a for loop? object buffer = new object; //...fill buffer. foreach...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: toton | last post by:
Hi, In some cases when my function returns, I need to return a object of null state. This is when I return object by value. (Just like for by pointer, I can return a null pointer ). It has to be...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
1
by: grbgooglefan | last post by:
I am in a perculiar situation. I want to use PyRun_SimpleString for creating Python functions in embedded Python in C++. But there could be cases when Python function code compilation could fail &...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.