Friday 11 July 2014

why we have to use “Using” in Sharepoint Object Model Code


Ans)SPSite, SPWeb: Several of the Windows SharePoint Services objects, primarily the SPSite class and SPWeb class objects, are created as managed objects. However, these objects use unmanaged code and memory to perform the majority of their work. The managed part of the object is much smaller than the unmanaged part. Because the smaller managed part does not put memory pressure on the garbage collector, the garbage collector does not release the object from memory in a timely manner. The object’s use of a large amount of unmanaged memory can cause some of the unusual behaviors described earlier. Calling applications that work with IDisposable objects in Windows SharePoint Services must dispose of the objects when the applications finish using them. You should not rely on the garbage collector to release them from memory automatically.
You can employ certain coding techniques to ensure object disposal. These techniques include using the following in your code:
Dispose method
using clause
try, catch, and finally blocks
SPContext:SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code.