3

I'm implementing IDataReader and I wonder if the implementation of Dispose is supposed to call Close or not.

Also, should Close call Dispose?

My guess is that Close shouldn't call Dispose and Dispose can call Close since AFAIK you should be able to call Dispose as many times as you want in any object. But this is just a guess and I'd rather hear an expert's opinion.

Juan
  • 15,274
  • 23
  • 105
  • 187

4 Answers4

6

Close() and Dispose() should do the same thing (one should call the other).
However, Close() does not need to be callable multiple times.

You should inherit the base DbDataReader class instead of re-implementing the whole thing yourself; if so, you'll need to override the abstract Close().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

.Dispose() should do exactly one thing: release unmanaged resources. Therefore you should call .Close() if and only if calling that function is the correct way to release an unmanaged resource. In this case, I would say that is true (it will release a database connection), and so you should call the function.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

They are both almost the same thing except Close can be called more than 1 time without getting an exception.

If you use the object more than once, implement .Close, if only once, implement .Dispose().

SventoryMang
  • 10,275
  • 15
  • 70
  • 113
-1

IDataReader is in interface. It doesn't dictate implementation.

John Saunders
  • 160,644
  • 26
  • 247
  • 397