What is IDisposable for?

Garbage collection is for memory. You need to dispose of non-memory resources – file handles, sockets, GDI+ handles, database connections etc. That’s typically what underlies an IDisposable type, although the actual handle can be quite a long way down a chain of references. For example, you might Dispose an XmlWriter which disposes a StreamWriter it … 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

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

Do I need to Dispose a SemaphoreSlim?

If you access the AvailableWaitHandle property, then Yes, you must call Dispose() to cleanup unmanaged resources. If you do not access AvailableWaitHandle, then No, calling Dispose() won’t do anything important. SemaphoreSlim will create a ManualResetEvent on demand if you access the AvailableWaitHandle. This may be useful, for example if you need to wait on multiple … Read more

What does Process.Dispose() actually do?

Do I really need to Dispose() every Process object and how do I decide if I need to do so? Yes, you should dispose them. Note this text in the documentation for Process: A system process is uniquely identified on the system by its process identifier. Like many Windows resources, a process is also identified … Read more

When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?

So it seems like the calling code needs to know about and take ownership of the response message as well as the stream, or I leave the response message undisposed and let the finalizer deal with it. Neither option feels right. In this specific case, there are no finalizers. Neither HttpResponseMessage or HttpRequestMessage implement a … Read more

Questions about Entity Framework Context Lifetime

Let’s get controversial! I disagree with the general MVC + EF consensus that keeping a context alive throughout the entire request is a good thing for a number of reasons: Low performance increase Do you know how expensive creating a new database context is? Well… “A DataContext is lightweight and is not expensive to create” … Read more

Manually destroy C# objects

You don’t manually destroy .Net objects. That’s what being a managed environment is all about. In fact, if the object is actually reachable, meaning you have a reference you can use to tell the GC which object you want to destroy, collecting that object will be impossible. The GC will never collect any object that’s … Read more