Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 812551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:13:17+00:00 2026-05-15T01:13:17+00:00

The output from the following program is: Non-Static Static Non-Static Is this a compiler

  • 0

The output from the following program is:

Non-Static
Static
Non-Static

Is this a compiler bug? I expected:

Static
Non-Static
Non-Static

because I thought the static constructor was ALWAYS called before the non-static constructor.

I tested this with Visual Studio 2010 using both .net 3.5 and .net 4.0.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StaticConstructorBug
{
    class Program
    {
        static void Main(string[] args)
        {
            var mc = new MyClass();

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public MyClass()
        {
            Console.WriteLine("Non-static");
        }

        static MyClass()
        {
            Console.WriteLine("Static");
        }

        public static MyClass aVar = new MyClass();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:13:17+00:00Added an answer on May 15, 2026 at 1:13 am

    See ECMA 334 §17.4.5.1:

    17.4.5.1 Static field initialization

    The static field variable initializers
    of a class declaration correspond to a
    sequence of assignments that are
    executed in the textual order in which
    they appear in the class declaration.
    If a static constructor (§17.11)
    exists in the class, execution of the
    static field initializers occurs
    immediately prior to executing that
    static constructor. Otherwise, the
    static field initializers are executed
    at an implementation-dependent time
    prior to the first use of a static
    field of that class

    Specifically: “execution of the static field initializers occurs immediately prior to executing that static constructor”.

    Your static MyClass aVar must be initialized before your static constructor executes (or, at least, it must appear that way). Without that static member, the static constructor should be called before any non-static constructors.

    If you still want a MyClass singleton, you can put it in a container class and refer to it using that, e.g.:

    public static class MyClassSingleton
    {
        public static MyClass aVar = new MyClass();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
This is output from my program sending agent update Created new player Identified sending
The output from the following: import feedparser d = feedparser.parse('http://www.netflix.com/NewWatchInstantlyRSS') d.entries[177].keys() is: ['summary_detail', 'links',
From what I understand, the output from the following script should include Win32_PerfRawData_PerfDisk_PhysicalDisk in
Consider the following output from a Tomcat server under Eclipse: INFO: Initializing Coyote HTTP/1.1
Hi I have following XAML code which is the output from XamlWriter.Save(): <StackPanel Name=itemStack
I'm using the following to execute a process and hide its output from Python.
Im using the following code to output all the nodes from the xml shown.
I've got the following code to output a list of items from amazon, but
Describe the output of the following SQL query: select custId, name from customer where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.