I'm doing a simple overloading of a function and I create a SPSite object when I'm calling the second function. I'm not sure of how to dispose of it in this scenario.

public static string GetConfigValue(string name, string webURL)
   return GetConfigValue(configName, new SPSite(webURL).RootWeb);

Like this :

public static string GetConfigValue(string configName, string webURL)
   string returnVal = null;

   using (SPSite site = new SPSite(webURL)) {
      returnVal = GetConfigValue(configName, site.RootWeb);

   return returnVal;

That assumes that you have another overloaded version of GetConfigValue(string, SPWeb).

You should be aware that instantiating SPSite and SPWeb objects in utility functions is a horribly inefficient way to code and will perform poorly under stress.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.