-4

Suppose we have a static constructor, a normal constructor and a static method in a class.

And in Main() we have code as:

Classname.staticmethod();

In this case, which constructor is called 1st before executing static method?:- the static constructor or normal constructor ?

variable
  • 8,262
  • 9
  • 95
  • 215
  • Short answer: [Yes](http://stackoverflow.com/questions/1437352/when-is-a-static-constructor-called-in-c). – Jashaszun Aug 01 '14 at 20:20
  • The static constructor would be invoked, not the instance constructor, unless static initialization created an instance. – Tim M. Aug 01 '14 at 20:20

5 Answers5

5

Why don't you try it ?

class Foo 
{

    static Foo() { Console.WriteLine("Static constructor is called."); }

    public Foo() { Console.WriteLine("The constructor is called."); }

    public static void Bar() { Console.WriteLine("The static Bar method is called."); }
}

public static void Main()
{
    Foo.Bar();
}

The output is:

// Static constructor is called.
// The static Bar method is called.

The MSDN says about static constructor:

It is called automatically before the first instance is created or any static members are referenced.

The instance constructor isn't called because you didn't create an instance.

For more information about the execution order of constructors have a look at this question.

Community
  • 1
  • 1
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

The static constructor will always be called - whether you call a static method, or create an instance.

Assuming a single default instance constructor, it will be called when call new Classname();

David Crowell
  • 3,711
  • 21
  • 28
1

The static constructor is only called once automatically. It will be called immediately after a static method is executed, or an instance is created. Whichever comes first.

So in this case, a static constructor would be called immediately because you called the static method. You can think of it as creating an instance of the static class

Andrew
  • 3,393
  • 4
  • 25
  • 43
  • Suppose I have both static and instance constructor. Then 1. When creating new object for 1st time the static constructor will run then the instance constructor will run? 2.incase of static method call for 1st time then will the static or the instance constructor be called? – variable Aug 01 '14 at 20:24
  • Yes. The static constructor is only called once automatically. It will be called as soon as: A static method is executed, or an instance is created. – Andrew Aug 01 '14 at 20:26
1

From Static Constructors (C# Programming Guide)

Static constructors have the following properties:

• A static constructor does not take access modifiers or have parameters.

• A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

• A static constructor cannot be called directly.

• The user has no control on when the static constructor is executed in the program.

• A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

• Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

• If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Here's a quick sample I wrote up to demonstrate what happens:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Classname.staticmethod();

            Classname name = new Classname();
            Console.ReadLine();
        }
    }
    public class Classname
    {
        public static void staticmethod()
        {
            Console.WriteLine("staticmethod called");
        }

        static Classname()
        {
            Console.WriteLine("Static Constructor called");
        }

        public Classname()
        {
            Console.WriteLine("Instance Constructor called");
        }
    }
}

Output:

Static Constructor called

staticmethod called

Instance Constructor called

jordanhill123
  • 4,142
  • 2
  • 31
  • 40
0

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91