Disposing and Nulling.

  • Thread starter Thread starter 455
  • Start date Start date
4

455

Hello.

I'm quite new to C# and am wondering which things I need to clean up when my
code completes, if any at all.

For example...

Assembly a = Assembly.Load(ServiceName);

Type mm = a.GetType(AssemblyName);

object o = Activator.CreateInstance(mm);

bject [] par = new object[] {xDoc};

string sReturn = "";

sReturn = (String) mm.InvokeMember("Execute",BindingFlags.Default |
BindingFlags.InvokeMethod,null,o,par);

//TODO: Do I need to dispose these things?

//((IDisposable)mm).Dispose();

mm = null;

a = null;

o = null;

par = null;



Is this the right way to do it? Or do I use my (IDisposable)x.Dispose()?
And if so, what is it doing?
 
C# is a managed language so you do not have to 'manually' dispose of objects
when you are done with them. Instead, when an object goes out of scope, the
memory manager elects the object for deletion.
 
If the variables are class level, and the class is used throughout the
program, nulling the variables will allow them to be garbage collected much
sooner than if otherwise, since there won't be a reference to them hanging
around.

Chris

455 said:
Ok... That sounds great, thanks.

To clarify, in VB, which was also "managed", it was always recommended to
get rid of your variables when your done with them, even though they would
also be deleted when they went out of scope. Is it still a good practice to
do in C#?

Karen Albrecht said:
C# is a managed language so you do not have to 'manually' dispose of objects
when you are done with them. Instead, when an object goes out of scope, the
memory manager elects the object for deletion.

--
Karen
This posting is provided "AS IS" with no warranties, and confers no rights.

455 said:
Hello.

I'm quite new to C# and am wondering which things I need to clean up
when
my
code completes, if any at all.

For example...

Assembly a = Assembly.Load(ServiceName);

Type mm = a.GetType(AssemblyName);

object o = Activator.CreateInstance(mm);

bject [] par = new object[] {xDoc};

string sReturn = "";

sReturn = (String) mm.InvokeMember("Execute",BindingFlags.Default |
BindingFlags.InvokeMethod,null,o,par);

//TODO: Do I need to dispose these things?

//((IDisposable)mm).Dispose();

mm = null;

a = null;

o = null;

par = null;



Is this the right way to do it? Or do I use my (IDisposable)x.Dispose()?
And if so, what is it doing?
 
455 said:
I'm quite new to C# and am wondering which things I need to clean up when my
code completes, if any at all.

For example...

Assembly a = Assembly.Load(ServiceName);
Type mm = a.GetType(AssemblyName);
object o = Activator.CreateInstance(mm);
object [] par = new object[] {xDoc};

string sReturn = "";

sReturn = (String) mm.InvokeMember("Execute",BindingFlags.Default |
BindingFlags.InvokeMethod,null,o,par);

//TODO: Do I need to dispose these things?

No - none of them implement IDisposable. However, if you had something
like a stream, which *does* implement IDisposable, it's worth calling
Dispose on that, which I prefer to do implicitly with the "using"
construct:

using (FileStream fs = ...)
{
....
}

There's no need to set local variables to null at the end of methods;
just letting them fall out of scope is good enough.
 
It's a good practice to dispose all your database access objects explicitly,
otherwise you might see growing number of database connections and database
operations will eventually fail. I have personal experience with that..

Eliyahu
 
Back
Top