Should i use idisposable




















The service may go out of scope at some arbitrary point in the future, and I don't want it hanging around not being GC'ed. Upon getting rid of that child view, the owner will call Dispose on it to give it the chance to unhook any event handlers. Here is a slightly contrived and very pseudo code example, note how the interface for the child view also implements IDisposable. There are far more authoritative comments about this from others on SO, like Do event handlers stop garbage collection from occuring?

You may also want to check out this codeproject article. I think the docs are pretty clear about what IDisposable is good for. The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

It even has an example. In that example the class that implements IDisposable contains a handle. A handle needs to be freed when it no longer used. This is done in the Dispose method. So the user of that class sees that it implements IDisposable and knows that the class needs to be explictily disposed when it is no longer needed so the handle can be freed.

It is a best practise i. You should implement IDisposable when your class holds resources that you want to release when you are finished using them. When your class contains unmanaged objects, resources, opened files or database objects, you need to implement IDisposable. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Apple has a finalizer to make sure it cleans up the Core resource. Apple allows early cleanup by calling Dispose.

The reason that you do not need Dispose and certainly do not need a finalizer is because your class Test does not own any member field that is unmanaged or IDisposable. You do happen to create a FileStream , which is disposable, but you clean it up before leaving the method. It is not owned by the Test object. There is one exception to this case. If you are writing a class that you know will be inherited by others, and the others may have to implement IDisposable , then you should go ahead and implement IDisposable.

Otherwise, callers won't know to dispose the object or even be able to without casting. However, this is a code smell. Usually, you would not inherit from a class and add IDisposable to it. If you do, it's probably poor design. As i wrote above that you need to implement IDisposable inorder to be able to use using. Now, when you implement IDisposable you get a Dispose method. In this method you write all the code that should take care of all the resources that needs to be disposed off when the object is no longer required.

The purpose of USING is that when an object goes out of its scope, it will call the dispose method and that is it.

I think that the question is more like "Should I dispose the file immediately or with the Dispose method of the class that accesses to that file? It depends: if you access the file only in the constructor in my opinion there is no reason to implement the IDisposable.

Ldloc, result ; il. Dup ; il. Sub ; il. Mul ; il. MarkLabel returnResult ; il. Assign result, Expression. GreaterThan value, Expression. MultiplyAssign result, Expression. Build - Release x64 Method IL ET Mahmood Ali. Only authorized users can answer the question. Please sign in first, or register a free account. Not the answer you're looking for?

Browse other questions tagged : c. Related Answers. Top Answers Related To c ,.



0コメント

  • 1000 / 1000