开发者

What does a static modifier on a constructor means?

I saw this kind of code at work:

class FooPlugin : IPlugin // IPlugin is a Microsoft CRM component, it has something special about it's execution
{
  static FooPlugin()
  {
     SomeObject.StaticFunction(); // The guy who wrote it said it's meaningful to this question but he开发者_开发技巧 can't remember why.
  }
}

Any idea what does a static modifier on a constructor mean and why in this case it is required?


This is the static initialization of the class.

It would be called when you use a method, a field, a property or anything else of the class. In other word, it would be called the first time you use the class.

See static constructors on MSDN

You can also initialize static stuff here.

In your example it seems that whoever wrote that wanted to call SomeObject.StaticFunction() once before people will use FooPlugin, probably so it will be initialized before using FooPlugin.

Note that there is some performance hit when you use it and visual studio (using code analysis) can let you know that you better off initialize the static fields inline.

See CA1810: Initialize reference type static fields inline on MSDN


It defines the static constructor for the object.

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Read more at MSDN - Static Constructors (C#)


I can't say why it's required in your particular case, but the motivation for a static constructor is usually one of these:

  1. All instances of the class need access to the same instance of an object, or
  2. Initialization of some external object is a prerequisite for all instances of the class (seems like the case in your example) or
  3. Initialization of some data structure or service takes takes too much time to do repeatedly (a variation of the first case).


Here's a simpler example of when a static constructor is useful. The following class has some static fields. The first can be initialized inline with its declaration, but the second one can't. Static constructor to the rescue. The key guarantee it provides is that no part of the class can be accessed before the initialization code runs.

class NeedsStaticConstructor
{

    private static Size s_size = new Size(100, 100); // can be done inline
    private static StringFormat s_format;            // more complex initialization needs code

    static NeedsStaticConstructor()
    {
        s_stateTextFormat = new StringFormat(StringFormatFlags.NoWrap);
        s_stateTextFormat.Alignment = StringAlignment.Near;
        s_stateTextFormat.LineAlignment = StringAlignment.Far;
    }
}


A static constructor is used to initialize class-wide (i.e. static) members, as opposed to instance members.


Just to add to the above answers, static constructor (or static blocks as in the case of Java) get executed only once when the class is first loaded in to memory. The objective is to initialize static fields, which otherwise would assume the respective default value of the data type. Sometimes I use static constructors to build an object model that I want to use throughout the life time of the application.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜