开发者

Static classes and efficiency

I want to understand the efficiency of static classes as I think my basic thinking in this area might be flawed. I often write code like the following with the assumption that the expensive reflection call will happen less frequently since the private variable will hold the information for the accessor. I'm pretty sure that this is good practice in non-static classes and instance properties but is there any benefit in using this construct in static classes or will the private field need to be instantiated on every call to the public accessor?

using System.Reflecti开发者_StackOverflow中文版on;
public static class ApplicationInformation
{
    public static Assembly ExecutingAssembly
    {
        get { return executingAssembly ?? (executingAssembly = Assembly.GetExecutingAssembly()); }
    }
    private static Assembly executingAssembly;
}


Why would it be instantiated on every call? It's a static field, it will "live" for as long as the AppDomain does, just like any other static field.

Admittedly I'd use typeof(ApplicationInformation).Assembly instead, which is probably cheaper... but that's a different matter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜