473,473 Members | 4,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Disposing and Nulling.

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?
Nov 15 '05 #1
4 19704
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" <no*@validaddress.com> wrote in message
news:xY***********************@news2.calgary.shaw. ca...
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?

Nov 15 '05 #2
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" <no*@validaddress.com> wrote in message
news:EM***********************@news1.calgary.shaw. ca...
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 [MSFT]" <ka*@online.microsoft.com> wrote in message
news:et*************@TK2MSFTNGP10.phx.gbl...
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" <no*@validaddress.com> wrote in message
news:xY***********************@news2.calgary.shaw. ca...
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?



Nov 15 '05 #3
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

"455" <no*@validaddress.com> wrote in message
news:xY***********************@news2.calgary.shaw. ca...
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?

Nov 15 '05 #4
455 <no*@validaddress.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
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
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...
0
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...
1
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
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,...
1
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...
0
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
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 ...
0
muto222
php
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.