I have a method, which has a try/catch/finaly
block inside. Within the try block, I declare SqlDataReader
as follows:
SqlDataReader aReader = null;
aReader = aCommand.ExecuteReader();
In the finally
block, the objects which are manually disposed of are those which are set at the class level. So objects in the method which implement IDisposable
, such as SqlDataReader
above, do they get automatically disposed of? Close()
is called on aReader
after a while loop executes to get the contents of the reader (which should be Dispose()
as that calls Close()
). If there is no call to Close()
, would this object be closed/disposed of automatically when the method finishes or the object goes out of scope?
EDIT: I am aware of the using
statement but there are scenarios which are confusing me.