11

Do I need to dispose a sqldatareader after it is created?

SqlDataReader reader;
---
---
---
reader.Close();
reader.Dispose();
Graham Clark
  • 12,886
  • 8
  • 50
  • 82
Paras
  • 2,997
  • 6
  • 35
  • 46
  • 1
    possible duplicate of [Dispose, when is it called?](http://stackoverflow.com/questions/2871888/dispose-when-is-it-called) – Aliostad May 05 '11 at 10:07

4 Answers4

34

Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown:

using (var reader = conn.ExecuteReader())
{
    ...
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    That is the general rule of thumb, however, it is not always feasible to do this. Some classes implement IDisposable somewhat needlessly, like some iterators. The iterator of a List, for example really would not need its dispose method called. Similarly, some WinForms controls really don't do anything in the dispose method, but they implement it because they must (due to inheritance. Lastly, a if the underlying unmanaged resource will be needed until the program ends (like a program's main form), you can gt away with not calling Dispose. – Kevin Cathcart May 06 '11 at 12:09
  • 1
    If a routine or class acquires ownership of an IDisposable object of an inheritable class, by any means other than a direct constructor call, it must allow for the possibility that the class of the object in question might require IDisposable.Dispose to be called. In most cases, it's simpler to ensure as a matter of habit that Dispose will get called, than to worry about the cases where one can get by without it. – supercat May 11 '11 at 19:23
2

Object which are Disposable can be best used (if possible) in a using block. At the end of the using block, the object is automatically disposed.

Because of memory management it is always advised do dispose your objects if you don't need them anymore.

Here's some stuff to read from the Microsoft's Docs.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Rhapsody
  • 6,017
  • 2
  • 31
  • 49
2

It is recommended to use the using pattern when dealing with anything that implements IDisposable

using ()
{
    // use it here
}

This will look after the try..catch..finally construct and calling Dispose.

EDIT I had previously said that I thought Close and Dispose did the same thing for readers (stream, file, sqldatareader etc.) but it appears this is not true looking at the documentation on SQLDataReader so my assumption was wrong.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • I don't think that Close disposes. But Dispose closes – Oskar Kjellin May 05 '11 at 10:11
  • @Oskar: that is an interesting discussion I think. If you look at the source code in .NET you see that some types behave the same when doing `Close` or `Dispose`, while others don't. I think it is safe to say that you shouldn't count on `Close` to properly free your resources. For instance, the `SqlConnection` does not call `GC.SuppressFinalize(this)` during a call to this, which force the object to live much longer than strictly needed. Therefore, you can conclude that your statement is true and the guideline holds: If an instance implements `IDisposable`: you should call dispose. – Steven May 05 '11 at 10:28
1

There is a simple guideline: if you have finished with an object whose type implements IDisposable then call Dispose() on it; you should use a using block to do this.

Richard
  • 106,783
  • 21
  • 203
  • 265