Why is there need for an explicit Dispose() method in asp.net MVC Controllers? Can anyone explain its intricacies? (asp.net specific)

Dispose is for releasing “unmanaged” resources (for example, sockets, file handles, Bitmap handles, etc), and if it’s being called outside a finalizer (that’s what the disposing flag signifies, BTW), for disposing other IDisposable objects it holds that are no longer useful. “Unmanaged” resources aren’t managed by the CLR (hence the name), and GC doesn’t mess … Read more

Who Disposes of an IDisposable public property?

There is no single answer, it depends on your scenario, and the key point is ownership of the disposable resource represented by the property, as Jon Skeet points out. It’s sometimes helpful to look at examples from the .NET Framework. Here are three examples that behave differently: Container always disposes. System.IO.StreamReader exposes a disposable property … Read more

Guidelines For Dispose() and Ninject

The CLR documentation states that whoever creates a Disposable object is responsible for calling Dispose. In this case the object is created by Ninject. That means you should not call Dispose explicitly. Ninject disposes every Disposable object that has another scope other than InTransientScope as soon as the scope object to which the created object … Read more

What’s the purpose of GC.SuppressFinalize(this) in Dispose() method?

When implementing the dispose pattern you might also add a finalizer to your class that calls Dispose(). This is to make sure that Dispose() always gets called, even if a client forgets to call it. To prevent the dispose method from running twice (in case the object already has been disposed) you add GC.SuppressFinalize(this);. The … Read more

Implementing IDisposable on a subclass when the parent also implements IDisposable

When I just override the Dispose(bool disposing) call, it feels really strange stating that I implement IDisposable without having an explicit Dispose() function (just utilizing the inherited one), but having everything else. This is something you shouldn’t be concerned with. When you subclass an IDisposable class, all of the “Dispose pattern” plumbing is already being … Read more

tech