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?

2
  • 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. Commented Jun 9, 2010 at 20:09
  • @SWeko: My bad you are right, I forgot what exactly was there. Commented Jun 9, 2010 at 20:09

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

6 Comments

Again, what does it mean? I don't know C# yet. In C++ you can't do something like this.
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?
@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?
I changed it. @brickner: What about why is it there? What does IPlugin and that call to the static function relate to it?
@Fyodor Soikin: The first version of the answer was only: "This is the static initialization of the class."
|
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#)

2 Comments

Duh but what does it mean exactly?
@the_drow - Duh. That's what the link is for. But I posted the relevant quote from MSDN.
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).

2 Comments

That's #2. Now I get it. That function initializes something within the ORM.
If you come from a C++ background, the equivalent in C++ is explained here: stackoverflow.com/questions/1197106/…
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; } } 

Comments

0

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.