SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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