4

Dupe of returning in the middle of a using block

Question title is fairly obvious I think, so given the following code is the SecurityDisabler disposed if true is returned?

public bool CreateProxyItem(string name, Sitecore.Data.ID sourceID, Sitecore.Data.ID targetID)
{
    // create an instance of the master database
    Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

    // get the proxy item template
    Sitecore.Data.Items.TemplateItem proxyTemplate = master.Templates[Sitecore.TemplateIDs.Proxy];

    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        // get a reference to the proxy container item
        Item proxyContainer = master.Items["/sitecore/system/proxies/"];

        if (proxyTemplate != null && proxyContainer != null)
        {
            Item proxyItem = proxyContainer.Add(name, proxyTemplate);

            if (proxyItem != null)
            {
                proxyItem.Editing.BeginEdit();
                // editing here
                proxyItem.Editing.EndEdit();

                return true;
            }
        }
    }

    return false;
}
Community
  • 1
  • 1
Nick Allen
  • 11,970
  • 11
  • 45
  • 58
  • Marked as closing as dupe. I know there's one somewhere... – Jon Skeet Apr 01 '09 at 09:50
  • 3
    I deleted my answer to respect the duplicate - but yes: the object is disposed. – Marc Gravell Apr 01 '09 at 09:52
  • You might also want to look at this one; at the IL level, the lock (try/finally) is virtually identical to the using (try/finally): http://stackoverflow.com/questions/266681/c-return-statement-in-a-lock-procedure-inside-or-outside/266718#266718 – Marc Gravell Apr 01 '09 at 09:56
  • Okay cheers guys, I did look through the list of suggested dupes but nothing came up, never mind! – Nick Allen Apr 01 '09 at 15:38

0 Answers0