Simple: Deleting Objects in C# F1 F1 F1...

  • Thread starter Thread starter Justine
  • Start date Start date
J

Justine

Hi All,

I have a very basic doubts about distroying an Object.
In VB.NET we use Nothing. What is the equivalent in C#.

kindly help me out...

Thanz in Advance.
Justine
 
I have a very basic doubts about distroying an Object.
In VB.NET we use Nothing. What is the equivalent in C#.

Please write 50 times:
- Setting a reference to Nothing does not destroy the object to which it
refers.

The equivalent in C# is:
myRef = null;
 
Justine said:
Hi All,

I have a very basic doubts about distroying an Object.
In VB.NET we use Nothing. What is the equivalent in C#.

myObject = null;

Fair warning: this does not destroy the object. The VB method doesn't
either.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Justine,

'Nothing' in vb is 'null' in C#.

Regarding everybody saying that it doesn't destroy the object, this is what
is meant, in case you don't already know this. "Managed code", which is what
C# and VB.NET are, suggests that the .NET Framework will destroy objects at
is own convenience, when it deems it appropriate. This doesn't do much for
optimized memory management, so unmanaged C++ is still ideal to a lot of
programmers, but it takes the pain away from those of us who want to avoid
messing with memory leaks and optimization and such.

In order for the .NET Framework's runtime engine garbage collector to delete
an object, the object must have already fallen out of scope of all
references that are themselves in scope. Marking a variable Nothing or null
*might* make an object flagged for deletion by the garbage collector, in
which case the object will be deleted *soon* but not *immediately*, but it
will not be marked for deletion if any other objects or other variables are
still referencing that object.

So if you have a function/method and you declare a variable within that
block and assign it a new object and neither that object nor its properties
are passed to any other variable anywhere, then you DON'T need to mark the
variable as 'Nothing' or 'null' in order for it to be deleted by the garbage
collector. It will be flagged for deletion when execution falls out of the
scope of that function/method. On the other hand, if the variable is
declared outside of the function/method, the object will be retained until
the variable is marked as 'Nothing' or 'null' or until the object containing
the variable itself becomes marked for deletion, or if it is a static class,
until the variable is marked as 'nothing' or 'null' or the application
terminates.

HTH,
Jon
 
Back
Top