The Dispose()
method if the IDisposal
interface is a specifc interface that is used when object locks heavy and expensive resources. For instance when you're acquiring a connection to a database. The Dispose method is there to tell the object that I'm done with the connection (in the Sql case) and that you can now close it and let someone else use the resource. In .NET everything is written in managed code and automatically garbage collected, whereas SQL connections and COM+ objects are not automatically released.
In the case of SharePoint it has been a discussion (for years now) on when and how to dispose objects. James explains it well with the rule of thumb. The reason you need to call Dispose in some cases is that SharePoint uses non-managed code (COM+) in SPSite
and SPWeb
objects to for instance make database calls. If the objects are not disposed properly you will eventually run out of memory since the garbage collector in .NET cannot automatically release the "external" (to .NET) objects.
In your case you should not use the using
statement since it will automatically dispose the object. The using statement is expanded like this in the compiler, from
using (SPWeb web = SPControl.GetContextWeb(Context)) {
...
}
to this:
try (SPWeb web = SPControl.GetContextWeb(Context)) {
...
}
finally {
web.Dispose();
}