5

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?

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
the_drow
  • 18,571
  • 25
  • 126
  • 193
  • 1
    actually, all modifiers are disallowed on a static constructor, so it will be just static FooPlugin(). Also this is invoked automatically, and there is no way to execute it on-demand. – SWeko Jun 09 '10 at 20:09
  • @SWeko: My bad you are right, I forgot what exactly was there. – the_drow Jun 09 '10 at 20:09

6 Answers6

6

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

brickner
  • 6,595
  • 3
  • 41
  • 54
  • Again, what does it mean? I don't know C# yet. In C++ you can't do something like this. – the_drow Jun 09 '10 at 20:08
  • Correct answer, but I'm a bit puzzled by `public` in that declaration. I always got a compile-time error when I accidentally specified access modifier on a static constructor. Did it change in C# 4.0? – Fyodor Soikin Jun 09 '10 at 20:10
  • 2
    @the_drow: Get C++ out of your head, your life will immediately get much better. Static constructor is a method that is called exactly once, the first time you attempt to use the class. What is unclear here? – Fyodor Soikin Jun 09 '10 at 20:11
  • I changed it. @brickner: What about why is it there? What does IPlugin and that call to the static function relate to it? – the_drow Jun 09 '10 at 20:11
  • @Fyodor Soikin: The first version of the answer was only: "This is the static initialization of the class." – the_drow Jun 09 '10 at 20:15
  • Yes except the purpose of this in this case. JsonTune explained it well though. Add your thoughts as well and I'll accept it – the_drow Jun 09 '10 at 20:24
3

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#)

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
3

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).
JasonTrue
  • 19,244
  • 4
  • 34
  • 61
  • That's #2. Now I get it. That function initializes something within the ORM. – the_drow Jun 09 '10 at 20:14
  • 1
    If you come from a C++ background, the equivalent in C++ is explained here: http://stackoverflow.com/questions/1197106/static-constructors-in-c-need-to-initialize-private-static-objects – JasonTrue Jun 09 '10 at 20:20
1

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;
    }
}
bbudge
  • 1,127
  • 6
  • 7
0

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

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
0

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.

Bedasso
  • 1,652
  • 1
  • 14
  • 17