3

The following code does not call the class static constructor. Is this a bug or feature?

class Test
{
    static Test
    {
       //do stuff
    }
    public static AnotherClass ClassInstance { get; set; }
}

class Program
{
    public static void Main()
    {
        var x = Test.ClassInstance;
    }
}

I don't have a compiler right now, but this is what happened to me today. The static constructor is never called, but it is called when ClassInstance is a field instead.

EDIT: I understand that static constructor is called when first instance is created or a field is accessed. Isn't there a field behind the automatic implemented property?

I am looking for some explanation on why the property does not trigger static constructor when property is implemented as two functions and one field. It is just very unlogical to me and that is why I thought it could be a bug.

7
  • 1
    I just compiled and ran your code. The static constructor does get called. Commented Nov 12, 2010 at 10:44
  • @Joe what framework are you targeting? Commented Nov 12, 2010 at 10:46
  • @Simone I tried targeting every framework available in VS2010 (namely 2.0 and higher) and it works the same in all. Commented Nov 12, 2010 at 10:52
  • I see, I'm using SharpDevelop and the exhibited behaviour is the same as the OP. Commented Nov 12, 2010 at 10:54
  • I was using vs2008 and .net 3.5 Commented Nov 12, 2010 at 10:58

4 Answers 4

1

Static constructors invoked the first time an instance of a class are created or when a static member is referenced. So the first time your create an instance of Test or when the ClassInstance property is referenced is when your static constructor will be called.

Would you like to know more? - http://msdn.microsoft.com/en-us/library/k9x6w0hc(VS.80).aspx

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

Comments

1

I verified the same behaviour, but if you change the code like this:

class AnotherClass {}

class Test
{
    static Test()
    {
        Console.WriteLine("Hello, world!");
    }

    public static AnotherClass ClassInstance { get { return new AnotherClass(); } }

}

class Program
{
    public static void Main()
    {
        var x = Test.ClassInstance;
    }
}

it writes "Hello, world!"...

Comments

1

Static constructor is called when any static member is accessed or instance is created

class Program
{
    static void Main(string[] args)
    {
        A.SomeField = new B();
    }
}

class A
{
    static A()
    {
        Console.WriteLine("Static A");
    }

    public static B SomeField { get; set; }
}

class B
{
    static B()
    {
        Console.WriteLine("Static B");
    }
}

Result:

Static B
Static A

As you see - there is no "Static B" in the result

4 Comments

@leiz: sure, there is a field and 2 methods generated automatically, but how is this related to static constructor? :-S
The example is not really explaining what I was asking for. B is never accessed therefore the static contructor is not called.
why is the static constructor not called when the field is accessed inside the property?
I am sorry, but I don't think that is what I am looking for. I will edit my question to make it clear hopefully.
0

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

more information from Msdn.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.