C#: Does dispose run when you are returning inside a using block?

Consider the code below. I wanted to confirm that the dispose method on an idisposable object got executed when you returned within an using statement. The anwser is yes: the dispose method runs before execution is passed to the caller method.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(callTestUsing.testUsing());
    }
}
public class callTestUsing
{
    public static string testUsing()
    {
        using (MyDisposingClass myDispose = new MyDisposingClass())
        {
            return "Return from testusing";
        }
    }
}
public class MyDisposingClass : IDisposable 
{
    public void Dispose()
    {
        Console.WriteLine("Disposing");
    }
}

SharePoint: Closing SPWeb and SPSite objects

Within both SharePoint 2003 and 2007, SPWeb objects need to be explicitly closed or disposed of. I believe this is because SPWeb and SPSite object use unmanaged code, so .net garbage collection doesn’t ensure that such objects are removed from memory. If you don’t close these, you will get a memory leak.
On the majority of blog articles I have seen (and some msdn documentation e.g. http://msdn2.microsoft.com/en-us/library/ms473155.aspx), closing / disposal of SPWeb and SPSite objects does not occur. I can only speculate that this is because the memory leak encountered is not noticable, unless you are creating a lot of these objects.

SPSite mySite = new SPSite(mySiteUrl);
SPWeb myWeb = mySite.OpenWeb();
try
{
    // Do something...
}
finally
{
    if (myWeb != null)
    {
        myWeb.Close();
    }
    if (mySite != null)
    {
        mySite.Close();
    }
}

Alternatively, as SPSite and SPWeb implement IDisposable, you can use the Dispose method instead of the close method, or use a using block (which implicitly calls the Dispose method):

using (SPSite mySite = new SPSite(mySiteUrl))
{
    using (SPWeb myWeb = mySite.OpenWeb())
    {
        // Do something here ...
    }
}

See http://msdn2.microsoft.com/en-us/library/ms778813.aspx for more info. Also note that an SPList object does not have a close method.

C#: Using An Embedded Resource

Quicknote: To embed resources in an assembly you need to add the resource to the project and set the resources build action property to “Embedded Resource”. I normally add resources to a seperate folder named myresources (hence the myresources part of the manifestResource string in the example below). To access the resource use the following code (note the manifest resource string is case sensitive and is of the form namespace.folder.resourcename):

using System;
using System.Reflection;
using System.IO;
namespace embeddedresourcestest
{
    class Program
    {
        static void Main(string[] args)
        {
            string manifestResource = "embeddedresourcestest.myresources.myresource.txt"; 
            Assembly _assembly = Assembly.GetExecutingAssembly();
            Stream myStream = _assembly.GetManifestResourceStream(manifestResource);
            StreamReader textStreamReader = new StreamReader(myStream);
            Console.Write(textStreamReader.ReadToEnd()); 
        }
    }

ASP.NET: Getting Context

Reminder: Normally, in asp.net you get context from the current page object, however this is not always possible (e.g. if you are coding a separate dll that does not implicitly have context). To get context from the caller within the dll, HttpContext.Current. The example below shows mapping an image url to a filepath.

filePath = System.Web.HttpContext.Current.Server.MapPath(imgUrl);