I wanna to know ,if there’s a difference between :
IfxDataReader ifxDataReaders = ifxCommand.ExecuteReader();
using (ifxDataReaders)
{
if (ifxDataReaders.Read())
{
item = (int)ifxDataReaders[0];
}
ifxDataReaders.Close();
}
using(IfxDataReader ifxDataReaders = ifxCommand.ExecuteReader())
{
if (ifxDataReaders.Read())
{
item = (int)ifxDataReaders[0];
}
ifxDataReaders.Close();
}
The tempting statement is to say “the first one won’t dispose”, but it will.
The second tempting statement is to say “the first one won’t dispose if
ExecuteReaderthrows an exception”, but in this case neither will dispose.If an error occurs in
ExecuteReader, in both instancesifxDataReaderswill not have been assigned so theDisposecall will never resolve anyway. As an aside, the call toCloseis not actually required.Disposewill be called in both instances and I believe in the same circumstances.Variable scope will change. In the second instance,
ifxDataReadersis scoped to theusingwhereas in the firstifxDataReaderscan be used after theusing(although, you’d be using a disposed object at this point).The second form is preferred, to me it seems more explicit and is more readable.