The correct implementation of IDisposable

Does it make sense to protect yourself from using the class? I just tried adding the following two lines to all the methods of my class IDisposable:

if (disposed)
    throw new InvalidOperationException("Attempt to use disposed object!");

It just doesn't seem right.

+4
source share
2 answers

My best answer is it depends.

Is the one-time class only for one project?

  • Probably not (although the statement on the same effect may be wrong)

Is the class used by someone outside of you or a small team?

  • Probably yes

What are the side effects of accessing a located facility?

  • otherwise it closes exceptions, it will make your life easier to debug, but the end result is the same in most cases.
  • (.. ..),
  • undefined , , "", "" .
+3

, , , . dkackman, , , , , , . , Form, IsDisposed. , , .

0

Source: https://habr.com/ru/post/1534726/


All Articles