This is a mirror of official site: http://jasper-net.blogspot.com/

Back To Basics - Dispose Vs Finalize

| Thursday, November 17, 2011
Introduction

We have been using the Dispose method for disposing objects in .NET. For the same purpose, we may also use the Finalize method. In this article I will try to explain what the Dispose and the Finalize methods are and where to use the Finalize and where to use the Dispose. I will also try to explain the difference between them.

Dispose

Garbage collector (GC) plays the main and important role in .NET for memory management so programmer can focus on the application functionality. Garbage collector is responsible for releasing the memory (objects) that is not being used by the application. But GC has limitation that, it can reclaim or release only memory which is used by managed resources. There are a couple of resources which GC is not able to release as it doesn't have information that, how to claim memory from those resources like File handlers, window handlers, network sockets, database connections etc. If your application these resources than it's programs responsibility to release unmanaged resources. For example, if we open a file in our program and not closed it after processing than that file will not be available for other operation or it is being used by other application than they can not open or modify that file. For this purpose FileStream class provides Dispose method. We must call this method after file processing finished. Otherwise it will through exception Access Denied or file is being used by other program.
Close Vs Dispose

Some objects expose Close and Dispose two methods. For Stream classes both serve the same purpose. Dispose method calls Close method inside.

void Dispose()
{
      this.Close();
}

Here question comes, why do we need Dispose method in Stream. Having Dispose method will enable you to write below code and implicitly call dispose method and ultimately will call Close method.

using (FileStream file = new FileStream("path", FileMode.Open, FileAccess.Read))
{
      //Do something with file
}


Read more: C# Corner
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://www.c-sharpcorner.com/UploadFile/nityaprakash/back-to-basics-dispose-vs-finalize/

Posted via email from Jasper-net

2 comments:

Anonymous said...

Μonday night is pizza night аt ouг house, with Мom's homemade vegetable pizza at the top of the list of favorites. There are plenty of electric ovens that will still knock your socks off. Cover pizza with the sausage, bacon and scrambled eggs.
Feel free to visit my blog post :: pizza stone

Anonymous said...

Pleаse let me knoω if you're looking for a article author for your site. You have some really great posts and I think I would be a good asset. If you ever want to take some of the load off, I'd love
to write some articles foг your blog in exchange for a
link bacκ to mine. Plеase send me an e-mail if interested.
Many thanks!

Here іs my ωeb sitе - Same Day Payday Loans