Alex Rivera | Logout

When do we need to call Dispose() in dot net c#?

Asked 2011-05-05T10:03:47.807
13

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

SqlDataReader reader;
---
---
---
reader.Close();
reader.Dispose();
Edit
Report

2 Answers

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.

answered 2011-05-05T10:08:56.807
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.

answered 2011-05-05T10:09:55.050

Your Answer