I'm assuming you mean DbDataReader
"or one of its subclasses (eg SqlDataReader
), but you can adapt the response if I am engaging. >
Your first example is correct, there is no need to enter a Close()
.
The function of the using
command is to ensure that all IDisposable
is correctly finalized at the end of the block - usually, or through an exception. This means that the Dispose
method will be called, if it is not null of course (i.e. if there is exception during its creation, there is no way to terminate it).
using(var dr = CriarDisposable()) {
// código
}
It is equivalent to:
{
var dr = CriarDisposable();
try
{
// Código
}
finally
{
if (dr != null)
((IDisposable)dr).Dispose();
}
}
DbDataReader
implements the IDisposable
interface, and the documentation for your method Dispose
says:
Releases resources used by DbDataReader
and calls Close
.
That is, it is not necessary to call Close
manually, using
itself will take care of it for you.
P.S. If I am wrong, and you refer to another class called DataReader
, take a look at the documentation if it also implements IDisposable
, and what the Dispose
method does.