ASP.NET Object Disposal

  • Thread starter Thread starter mukulprabhu
  • Start date Start date
M

mukulprabhu

Hello there,

Is it compulsory that the component objects created in code behind
should be disposed explicitly.

For eg.

first.cs
index.aspx.cs
-----------------------------------------------------
-----------------------------------------------------
Class first first
objFirst = new first();
{
page_load()
method A () {some code} {
}
objFirst.A();
}

in the above example is it nessesary to dispose "objFirst" object.

Anticipating your reply.

Thanx in advance.
 
Mukul,

I never dispose my component objects explicitly. I wonder what gave you
the idea? I am assuming you are putting first.cs in App_Data (assuming
you are using .NET 2.0) but nevertheless the Garbage Collection should
handle this.

Sarat
 
Objects normailly do not need to be disposed unless they have resources or
referencess to other objects that have unmanaged resources that need to be
disposed (file handles, bitmaps, network streams, sockets, connections, etc.)

If your object has a Dispose method, you can call it, or have your class
implement the IDisposable pattern and it can call it.
Otherwise, objects are destroyed automatically by the runtime and in the
case of an ASP.NET page, everything goes away anyway as soon as the page
lifecycle is complete and the HTML has been sent out to the browser.

Peter
 
Back
Top