2
\$\begingroup\$

I wanted to avoid running the code HttpContext.Current.Server.MapPath every time file_path_includes is called.

Is the below an effective way of doing so?

private static string file_path_includes_;
public static string file_path_includes
{
    get
    {
        if (file_path_includes_ == null)
            file_path_includes_ = HttpContext.Current.Server.MapPath("/_includes") + @"\";
        return file_path_includes_;
    }
}

So the better implentation would be a static constructor as such:

public static string file_path_includes;

static ParentClassName()
{
    file_path_includes = HttpContext.Current.Server.MapPath("/_includes") + @"\";
}
\$\endgroup\$
2
  • \$\begingroup\$ This definitely looks like a valid way to lazily initialize a property, though I'm not a fan of the naming style. \$\endgroup\$
    – Magus
    Mar 13, 2014 at 15:53
  • \$\begingroup\$ Yes, that static constructor seems correct. However, I am not sure if HttpContext.Current.Server will be available at the time, the static constructor is called. If so, the earlier lazy option is preferable or you can even do away with the static member variable. \$\endgroup\$
    – hocho
    Mar 13, 2014 at 16:15

2 Answers 2

3
\$\begingroup\$

Is it best to initialize a static member once, in a static constructor. If that is not possible, then a lazy initialization can be done like this. (Similar code to yours, just using the null coalescing operator.)

private static string file_path_includes_;

public static string file_path_includes
{
    get
    {
        return 
            file_path_includes_ ??
                (file_path_includes_ = HttpContext.Current.Server.MapPath("/_includes") + @"\");
    }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Please demonstrate static constructor and ?? is completely novel to me. \$\endgroup\$ Mar 13, 2014 at 15:58
  • \$\begingroup\$ A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. ?? is the null coalescing operator. You can look it up. \$\endgroup\$
    – hocho
    Mar 13, 2014 at 16:00
  • 3
    \$\begingroup\$ If lazy initialization is desired, it is worth considering the Lazy<T> class. You may even want to refer to the MSDN page on Lazy Initialization. \$\endgroup\$
    – Dan Lyons
    Mar 13, 2014 at 17:10
  • \$\begingroup\$ @DanLyons SharePoint 2010 :( \$\endgroup\$ Mar 13, 2014 at 18:41
2
\$\begingroup\$

Since the file_path_includes remains the same so it makes sense to make it readonly. Instead of using a static constructor you can initalize it inline as it will make loading the variable even lazier.

private static readonly string file_path_includes = HttpContext.Current.Server.MapPath("/_includes") + @"\";

As Magus mentioned variable naming convention needs some thought.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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