Should all classes implement the IDisposable interface?

(very new questions)

I might be wrong, but on MSDN I find it a good practice to implement a Dispose destructor in every class that you write. Do I (really) have to implement an IDisposable interface with every class I write?

is also the correct syntax for implementing an interface for placing the keyword "implements" in the line after the class declaration? I put it on the same line as the "class" and I received an error message.

one more thing: when coding a method implemented by an interface, be sure to follow this syntax as an example:

Public Sub Dispose() Implements IDisposable.Dispose

what interests me in the code above is if I need to declare the implemented method as "Implements System.IDisposable.Dispose"

+3
source share
4 answers

You should IDisposableonly use it if your class contains instances of other classes that implement it IDisposable, or if it contains its own resources.

See this article for more information .

+10
source

No, not worth it. Only implement IDisposable if you use unmanaged resources directly or have members that implement IDisposable and you want their call method to be called when the dispose method is called in your class.

+3
source

, , "" . , . . - , ( , , ; - ).

IDisposable - , , , , . , , , , , ; ​​ , (1) , (2) IDisposable; IDisposable.Dispose, .

, IDisposable , - , IDisposable, , , IDisposable.Despose -. , , . , , : " , ". , , . , , , , . IDisposable. , " ".

+2

.Net framework ( ). ( , ). , , IDisposable. DataAccesslayer, IDisposable

, , iDisposable: https://msdn.microsoft.com/en-us/library/498928w2%28v=vs.110%29.aspx

0

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


All Articles