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

  • Home
  • SEARCH
  • 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 1039031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:02:58+00:00 2026-05-16T15:02:58+00:00

Another question on SO inspired me to try this code in C#: class Program

  • 0

Another question on SO inspired me to try this code in C#:

class Program
{
    static Program() 
    {
        new Program().Run();
    }

    static void Main(string[] args) { }

    void Run()
    {
        System.Console.WriteLine("Running");
    }
}

This prints “Running” when run.

I actually expected the compiler to complain about this. After all, if the class has not yet been initialized by the static constructor; how can we be sure that it is valid to call methods on it ?

So why does the compiler not restrict us from doing this ? Is there any important usage scenarios for this ?

Edit

I am aware of the Singleton pattern; the point in question is why I can call a method on the instance before my static constructor finishes. So far JaredPar’s answer has some good reasoning about this.

  • 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-16T15:02:59+00:00Added an answer on May 16, 2026 at 3:02 pm

    It is allowed because not allowing it would be a lot worse. Code like this would deadlock badly:

    class A {
        public static readonly A a;
        public static readonly B b;
        static A() {
            b = new B();
            a = B.a;
        }
    }
    
    class B {
        public static readonly A a;
        public static readonly B b;
        static B() {
            a = new A();
            b = A.b;
        }
    }
    

    You are of course pointing a loaded gun at your foot.

    This behavior is documented in the CLI Spec (Ecma 335) Partition II, Chapter 10.5.3.2 “Relaxed guarantees”:

    A type can be marked with the attribute beforefieldinit (§10.1.6) to indicate that the guarantees specified in §10.5.3.1 are not necessarily required. In particular, the final requirement above need not be provided: the type initializer need not be executed before a static method is called or referenced.

    [Rationale: When code can be executed in multiple application domains it becomes particularly expensive to ensure this final guarantee. At the same time, examination of large bodies of managed code have shown that this final guarantee is rarely required, since type initializers are almost always simple methods for initializing
    static fields. Leaving it up to the CIL generator (and hence, possibly, to the programmer) to decide whether this guarantee is required therefore provides efficiency when it is desired at the cost of consistency guarantees.
    end rationale]

    The C# compiler indeed emits the beforefieldinit attribute on a class:

    .class private auto ansi beforefieldinit ConsoleApplication2.Program
           extends [mscorlib]System.Object
    {
       // etc...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 541k
  • Answers 541k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use XPerf on Windows to tell you in excruciating detail… May 17, 2026 at 2:49 am
  • Editorial Team
    Editorial Team added an answer The immediate problem is that you have: <p class="prf_faves"> <img… May 17, 2026 at 2:49 am
  • Editorial Team
    Editorial Team added an answer Wow, that fits the "a little knowledge could be dangerous"… May 17, 2026 at 2:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Inspired by this question , I wanted to try my hand at the latest
This question was inspired by this answer to another question, indicating that you can
In another question, someone posted this code in an answer to my question. drupal_set_message(
I've been inspired by another question: Best Practice for Using Java System Properties I'm
Inspired by another question asking about the missing Zip function: Why is there no
I was inspired by another question to write a script (or rather a one-liner)
Another question of mine about optimizing Objective C programs inspired the following: does anyone
Inspired by this question on Meta , I wrote two queries on the Stack
Inspired by this question where there are differing views on SET NOCOUNT... Should we
This is stemmed from another question I had where the solution had me using

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.